phrity/slim-openapi

OpenApi implementation for Slim 4.

1.2.1 2022-10-11 16:36 UTC

This package is auto-updated.

Last update: 2024-04-12 20:10:43 UTC


README

Build Status Coverage Status

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.

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"
            }
        }
    }
}

Documentation

Versions

Version PHP
1.2 ^7.4|^8.0 Request/Response validation, YAML support
1.1 ^7.4|^8.0 Settings & helpers
1.0 ^7.4|^8.0 Route registry