Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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

```