radebatz / openapi-router
Routing adapter for OpenApi annotations.
3.0.1
2024-11-09 01:00 UTC
Requires
- php: >=8.1
- psr/log: ^1.1 || ^2.0 || ^3.0
- psr/simple-cache: ^1.0 || ^2.0 || ^3.0
- radebatz/openapi-extras: ^2.1
- slim/slim: ^4.14
- zircote/swagger-php: ^4.11.1
Requires (Dev)
- doctrine/annotations: ^2.0
- friendsofphp/php-cs-fixer: ^3.0
- nyholm/psr7: ^1.4
- nyholm/psr7-server: ^1.0
- phpunit/phpunit: ^9.0 || ^10.5
- symfony/cache: ^6.0 || ^7.0
- symfony/psr-http-message-bridge: ^6.0 || ^7.0
Suggests
- radebatz/openapi-verifier: Allows your PHPUnit tests to validate your controller response against your OpenAPI spec.
README
Introduction
Allows to (re-)use Swagger-PHP attributes (docblock annotations are deprecated), to configure routes in the following frameworks:
Requirements
- PHP 8.1 or higher - depending on framework version.
Installation
You can use composer or simply download the release.
Composer
The preferred method is via composer. Follow the installation instructions if you do not already have composer installed.
Once composer is installed, execute the following command in your project root to install this library:
composer require radebatz/openapi-router
After that all required classes should be availabe in your project to add routing support.
Basic usage
Example using the Slim
framework adapter and standard OpenApi attributes only.
Controller
<?php namespace MyApp\Controllers\V1; use OpenApi\Attributes as OA; use Radebatz\OpenApi\Extras\Attributes as OAX; /* Things shared by all endpoints in this controller.*/ #[OAX\Controller(prefix: '/api/v1')] #[OA\Response(response: 200, description: 'OK')] #[OAX\Middleware(names: ['auth', 'admin'])] class GetController { #[OA\Get(path: '/getme', operationId: 'getme')] #[OA\Response(response: 400, description: 'Not good enough')] public function getme($request, $response) { return $response->write('Get me'); } }
index.php
<?php use Radebatz\OpenApi\Routing\Adapters\SlimRoutingAdapter; use Radebatz\OpenApi\Routing\OpenApiRouter; use Slim\App; require '../vendor/autoload.php'; $app = new App(); (new OpenApiRouter([__DIR__ . '/../src/controllers'], new SlimRoutingAdapter($app))) ->registerRoutes(); $app->run();
Documentation
License
The openapi-router project is released under the MIT license.