Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/incapption-public/simpleapi
Simple API Framework for PHP
https://github.com/incapption-public/simpleapi
api-rest composer php-library php72
Last synced: 22 days ago
JSON representation
Simple API Framework for PHP
- Host: GitHub
- URL: https://github.com/incapption-public/simpleapi
- Owner: incapption-public
- Created: 2021-09-27T13:07:09.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-30T17:40:11.000Z (9 months ago)
- Last Synced: 2024-10-15T19:22:27.809Z (22 days ago)
- Topics: api-rest, composer, php-library, php72
- Language: PHP
- Homepage:
- Size: 57.6 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SimpleApi
Tiny package for setting up a simple REST API with PHP.
Ability to group routes and define middlewares on a group or route level.## Usage
1. Implement the iApiController interface with your controller. A method can return `StringResult`, `DataResult` or `ErrorResult` which always return arrays with different structure based on the result.
```php
get()');
}public function index(ApiRequest $request): iMethodResult
{
return new StringResult(HttpStatusCode::SUCCESS(), 'TestController->index()');
}public function create(ApiRequest $request): iMethodResult
{
return new StringResult(HttpStatusCode::SUCCESS(), 'TestController->create()');
}public function update(ApiRequest $request): iMethodResult
{
return new StringResult(HttpStatusCode::SUCCESS(), 'TestController->update()');
}public function delete(ApiRequest $request): iMethodResult
{
return new StringResult(HttpStatusCode::SUCCESS(), 'TestController->delete()');
}
}
```2. Extend the `SimpleApi` class. Here you can group and register your routes.
```php
registerRoutes();
}protected function registerRoutes()
{
// A group which always requires authentication
SimpleApiRoute::registerGroup('user', [new AuthenticationMiddleware()]);// A public endpoint not requiring authentication. The 'public' group is defined without middleware.
SimpleApiRoute::registerGet('public', '/api/currencies', TestController::class, 'get');// The middleware defined for group 'user' above will be executed when calling this route.
SimpleApiRoute::registerGet('user', '/api/user/{userId}/files', TestController::class, 'get');// Example for a group which might require user authentication middleware
SimpleApiRoute::registerGet('internal', '/api/schema', TestController::class, 'get', [new AuthenticationMiddleware()]);
}
}
```3. In your application root (e.g. app.php) use this controller to verify if the request is an API request, call the method and return the result.
```php
use Incapption\SimpleApi\Helper\HttpHeader;
use Incapption\SimpleApi\Helper\HttpPayload;use Incapption\SimpleApi\Tests\SimpleApiExtension;$api = new SimpleApiExtension($_SERVER["REQUEST_URI"], $_SERVER["REQUEST_METHOD"], HttpHeader::getAll(), HttpPayload::getInput());
// Check if the actual request is targeted at your API endpoint
if ($api->isApiEndpoint('/api/v1'))
{
$result = $api->getResult();
// Returns the result as JSON and exits the application
$api->sendAndTerminate($result);
}
```The returned JSON format is like this depending on whether the Controller method returns a `StringResult`, `DataResult` or `ErrorResult` object:
Example for `DataResult` which returns an object of unknown structure.
```json
{
"name": "John Doe",
"age": 27
}
```Example for return type `StringResult`, which always returns a `message` of type `string`
```json
{
"message": "This request was a success"
}
```Example for return type `ErrorResult`, which always contains an `errors` key which contains either a string or list of errors.
```json
{
"errors": "User ID is required"
}
``````json
{
"errors": {
"user_id": [
"Must be of type string"
]
}
}
```## Tests
Create a `phpunit.xml` in the project directory. Run PHPUnit referencing this xml. For example:
`php.exe ./SimpleApi/vendor/phpunit/phpunit/phpunit --configuration .\SimpleApi\phpunit.xml --teamcity````xml
src/
tests
```