Middlewares for Apitte.
Transform and validate request or early return response before it is handled by dispatcher.
First of all, setup core and contributte/middlewares packages.
Install and register middlewares plugin
composer require apitte/middlewares
api:
plugins:
Apitte\Middlewares\DI\MiddlewaresPlugin:
In index.php
replace Apitte\Core\Application\IApplication
with Contributte\Middlewares\Application\IApplication
.
TracyMiddleware (with priority 100) and AutoBasePathMiddleware (with priority 200) are registered by default, but you could disable them if you want.
api:
plugins:
Apitte\Middlewares\DI\MiddlewaresPlugin:
tracy: true
autobasepath: true
Apitte\Middlewares\ApiMiddleware
which run whole Apitte application is registered with priority 500. Make sure there is no middleware with higher priority.
If you want to add another middleware, just register a class with appropriate tags.
services:
m1:
factory: App\Api\Middleware\ExampleMiddleware
tags: [middleware: [priority: 10]]
namespace App\Api\Middleware;
use Contributte\Middlewares\IMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class ExampleMiddleware implements IMiddleware
{
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
{
// Call next middleware in a row
$response = $next($request, $response);
// Return response
return $response;
}
}
See contributte/middlewares documentation for more info and useful middlewares