phrity / slim-openapi
OpenApi implementation for Slim 4.
1.2.1
2022-10-11 16:36 UTC
Requires
- php: ^7.4 | ^8.0
- cebe/php-openapi: ^1.4
- league/openapi-psr7-validator: ^0.15
- psr/container: ^1.0 | ^2.0
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- slim/slim: ^4.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- php-di/php-di: ^6.0
- phpunit/phpunit: ^9.0
- slim/psr7: ^1.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-10-12 21:20:05 UTC
README
OpenApi for Slim v4
Adapter that reads OpenApi schema and add included routes to Slim.
By defining operationId
in OpenApi schema, the adapter will automatically instanciate and call referenced controller class.
Some features
- Automatic mapping of routes to controllers
- OpenApi specification in JSON or YAML source
- Optional validation of requests and responses
Installation
Install with Composer;
composer require phrity/slim-openapi
How to use
use Phrity\Slim\OpenApi; use Slim\Factory\AppFactory; // Create Slim App as you normally would $slim = AppFactory::create(); // Create OpenApi adapter with OpenApi source $openapi = new OpenApi('openapi.json'); // Push all routes from OpenApi to Slim $openapi->route($slim); // Run Slim $slim->run();
How to define controllers
In order for automatic mapping to work, the operationId
must be set on all defined routes in OpenApi source.
If no method is specified, class method __invoke()
will be called on class.
Example
{ "openapi": "3.0.0", "paths": { "/test": { "get": { "operationId": "Test/MyController", "description": "Will invoke on class Test\\MyController" }, "put": { "operationId": "Test\\MyController:put", "description": "Will call method put() on class Test\\MyController" } } } }