Build Status Coverage Status

Adapter that reads OpenApi schema and add included routes to Slim.

Current version supports PHP ^7.4 || ^8.0.

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);

How to define controllers

In order for automatic mapping to work, the operationId must be set on all defined routes in OpenApi source. The following definitions are supported;

  • Classname
  • Namespace/Classname
  • Namespace\\Classname
  • Classname:httpMethod
  • Namespace/Classname:httpMethod
  • Namespace\\Classname:httpMethod

Example

{
    "openapi": "3.0.0",
    "info": {},
    "paths": {
        "/test": {
            "get": {
                "operationId": "Test/MyController",
                "description": "Will invoke class Test\MyController"
            },
            "put": {
                "operationId": "Test\\MyController:put",
                "description": "Will call method put() on class Test\MyController"
            }
        }
    }
}