A neat little framework for creating PHP APIs

1.0.0 2017-12-08 21:36 UTC

This package is auto-updated.

Last update: 2024-11-25 23:04:57 UTC


README

Build status Code coverage Code quality Latest version Downloads total License

Example usage

Setting up is really simple.

You'll need a routing config:

test:
    method: [GET, POST]
    path: /
    handler: MatthiasMullie\ApiExample\ExampleController

A few lines to bootstrap, in a file where all of your requests end up:

$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:

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.

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:

composer require matthiasmullie/php-api

Although it's recommended to use Composer, you can actually include these files anyway you want.

License

php-api is MIT licensed.