radebatz/openapi-router

Routing adapter for OpenApi annotations.

3.0.1 2024-11-09 01:00 UTC

This package is auto-updated.

Last update: 2024-12-10 21:04:18 UTC


README

Build Status Coverage Status License: MIT

Introduction

Allows to (re-)use Swagger-PHP attributes (docblock annotations are deprecated), to configure routes in the following frameworks:

Requirements

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.