Build PSR-15 middleware pipelines in Mezzio: error handling, routing, auth, and dispatch.
Mezzio — PSR-15 Middleware Stack
composer create-project mezzio/mezzio-skeleton myapp
// config/pipeline.php
$app->pipe(ErrorHandler::class);
$app->pipe(ServerUrlMiddleware::class);
$app->pipe(RouteMiddleware::class);
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(AuthenticationMiddleware::class);
$app->pipe(DispatchMiddleware::class);
$app->pipe(NotFoundHandler::class);
// Handler (controller replacement)
class PostHandler implements RequestHandlerInterface {
public function handle(ServerRequestInterface $request): ResponseInterface {
$posts = $this->postTable->fetchAll();
return new JsonResponse(["posts" => $posts]);
}
}
// Route configuration
return function(Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
$app->get("/api/posts", PostHandler::class, "api.posts");
$app->post("/api/posts", CreatePostHandler::class);
$app->get("/api/posts/{id}", PostDetailHandler::class);
};