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.
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.
With invoke | With method |
---|---|
Classname |
Classname:method |
Namespace/Classname |
Namespace/Classname:method |
Namespace\\Classname |
Namespace\\Classname:method |
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"
}
}
}
}
Settings
Setting | Default | Description |
---|---|---|
strict |
false |
If true , will validate OpenApi schema and throw exception on error |
controller_prefix |
"" |
Prefix operationId when creating controller class name |
controller_method |
false |
Add current HTTP method (get, put, etc) if not specified in schema |
Example
$openapi = new OpenApi('openapi.json', [
'strict' => true,
'controller_prefix' => 'Test/',
'controller_method' => true,
]);
{
"openapi": "3.0.0",
"paths": {
"/test": {
"get": {
"operationId": "MyController",
"description": "Will call method get() on class Test\\MyController"
},
"put": {
"operationId": "MyController:myMethod",
"description": "Will call method myMethod() on class Test\\MyController"
}
}
}
}
Versions
Version | PHP | |
---|---|---|
1.1 |
^7.4|^8.0 |
Settings & helpers |
1.0 |
^7.4|^8.0 |
Route registry |