php-api
, (*1)
Example usage
Setting up is really simple., (*2)
You'll need a routing config:, (*3)
test:
method: [GET, POST]
path: /
handler: MatthiasMullie\ApiExample\ExampleController
A few lines to bootstrap, in a file where all of your requests end up:, (*4)
$routes = new MatthiasMullie\Api\Routes\Providers\YamlRouteProvider(__DIR__.'/../config/routes.yml');
$handler = new MatthiasMullie\Api\RequestHandler($routes);
$response = $handler->route(GuzzleHttp\Psr7\ServerRequest::fromGlobals());
$handler->output($response);
And a controller:, (*5)
namespace MatthiasMullie\ApiExample;
class ExampleController implements MatthiasMullie\Api\Controllers\JsonController
{
public function __invoke(Psr\Http\Message\ServerRequestInterface $request, ResponseInterface $response, array $args)
{
// hey there, I can process your request!
return [
'status_code' => 200,
'hello' => 'world',
];
}
}
Or take a look at this exact same example in a clean repo, at
matthiasmullie/php-api-example., (*6)
Installation
Simply add a dependency on matthiasmullie/php-api to your composer.json file
if you use Composer to manage the dependencies of
your project:, (*7)
composer require matthiasmullie/php-api
Although it's recommended to use Composer, you can actually include these files
anyway you want., (*8)
License
php-api is MIT licensed., (*9)