Middleware
The documentation is constantly evolving and updated alongside the project's progress. Since it is developed in parallel with the code, some sections may temporarily refer to earlier versions of the system-these are gradually adapted to the current state of the application. If you notice any outdated information, please report it or open an Issue / Pull Request.
Documentation
# Middleware
Middleware implements Psr\Http\Server\MiddlewareInterface. It receives a server request and a request handler, may inspect or decorate the request, and either returns a response or delegates to the next handler.
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
final class ExampleMiddleware implements MiddlewareInterface
{
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
$request = $request->withAttribute('processed_by_example', true);
return $handler->handle($request);
}
}
# Middleware stack
The framework MiddlewareStack executes registered middleware in order and can limit a middleware entry to a URI prefix. Application bootstrap is responsible for registering middleware and any required services. For example, StartSessionMiddleware starts the session before downstream handlers run; ExceptionMiddleware converts unhandled exceptions into HTTP responses; CorsMiddleware handles cross-origin policy.
Keep middleware focused on cross-cutting HTTP concerns. Put business operations in services and endpoint-specific orchestration in controllers. Middleware that terminates a request should return a response without calling the next handler.