matthiasmullie / php-api
A neat little framework for creating PHP APIs
1.0.0
2017-12-08 21:36 UTC
Requires
- php: ^7.0
- guzzlehttp/guzzle: ^6.2
- guzzlehttp/psr7: ^1.3
- league/route: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.0
- phpunit/phpunit: ~6.0
- symfony/yaml: ^3.2|^4.0
Suggests
- symfony/yaml: Needed to support routes in YAML format
This package is auto-updated.
Last update: 2024-11-25 23:04:57 UTC
README
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.