Middleware Order in Laminas Mezzio: Put Error Handling First
In Laminas Mezzio, where you register error-handling middleware decides whether exceptions become structured errors or raw PHP failures. Check config/pipeline.php before deploying.
18 Apr 2026, 09:48 UTC

The problem: an exception becomes a raw PHP error
You add a new route to a Laminas Mezzio application, intentionally throw an exception inside the request handler, and expect your application’s JSON error response. Instead, the browser displays a raw PHP stack trace or a blank 500 page. The cause is usually not the exception itself. It is the position of your error-handling middleware inside the application pipeline.
Mezzio processes HTTP requests through a layered stack of PSR-15 middleware. PSR-15 is the PHP standard for server middleware: each middleware receives a PSR-7 request and a handler, then returns a PSR-7 response. PSR-7 defines the standard interfaces for HTTP request and response messages. Because the pipeline executes middleware in registration order, an error handler registered too late cannot catch exceptions thrown by middleware or handlers registered before it.
How pipeline order changes behavior
Think of the pipeline as an onion. The outermost middleware runs first on the way in and last on the way out. If you register error handling last, exceptions thrown by routing or dispatch bubble past it and reach PHP’s default error handler. If you register it first, it wraps everything downstream and can convert exceptions into consistent error responses.
This is a configuration decision with operational consequences. A misordered pipeline can hide failures behind generic error pages, complicate debugging, and leak stack traces to clients.
Worked example: register error handling before routing
Assuming Laminas Mezzio 3.x and PHP 8.1+, open config/pipeline.php. This file registers middleware on the Application instance using $app->pipe(). The following example shows a safe ordering. Replace the placeholder classes with your project’s actual middleware names.
<?php
declare(strict_types=1);
use App\Middleware\RequestLoggerMiddleware;
use Laminas\Mezzio\Application;
return function (Application $app): void {
// 1. Outermost: catch exceptions from everything downstream.
$app->pipe(\App\Middleware\ErrorHandlerMiddleware::class);
// 2. Cross-cutting concerns that should see every request.
$app->pipe(RequestLoggerMiddleware::class);
// 3. Routing: match the request to a route and handler.
$app->pipe(\Laminas\Mezzio\Middleware\RoutingMiddleware::class);
// 4. Dispatch: invoke the matched handler.
$app->pipe(\Laminas\Mezzio\Middleware\DispatchMiddleware::class);
};
PSR-15 middleware such as RequestLoggerMiddleware implements process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface. It can log the request, then delegate to the next layer:
<?php
declare(strict_types=1);
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class RequestLoggerMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
error_log(sprintf('%s %s', $request->getMethod(), $request->getUri()->getPath()));
return $handler->handle($request);
}
}
If your application uses a PSR-7 implementation other than Laminas Diactoros, keep the implementation consistent across middleware and handlers. Mixing PSR-7 message types can cause type errors when middleware expects one implementation and receives another.
Verify the order before you deploy
Run these checks in your project root. None require elevated permissions.
Confirm the installed package and version:
# Run in the project root. Read-only. composer show laminas/laminas-mezzioExpected: a line showing
laminas/laminas-mezzioand a 3.x version. Risk: none.Inspect registration order:
# Run in the project root. Read-only. grep -n "->pipe(" config/pipeline.phpExpected: error-handling middleware appears before routing and dispatch entries. Risk: none.
Exercise the pipeline with an HTTP request:
# Run while the app is served locally. Read-only HTTP request. curl -i http://localhost:8080/trigger-errorExpected: a status code and body produced by your error handler, not a raw PHP stack trace. If you see a raw trace, the error handler is likely registered too late or missing. Risk: none; this is a read-only request.
For a stricter check, write a test that boots the application, sends a request through the pipeline, and asserts the response status code. Do not assume the pipeline is correct because the application starts without errors.
Trade-off: manual ordering means manual mistakes
Mezzio does not enforce a canonical pipeline order. It also does not enforce CSRF protection by default; you must add it explicitly if your application requires it. The flexibility lets you tailor the stack to your application, but every new middleware requires a deliberate placement decision. Document the intended order in config/pipeline.php comments so the next developer does not move error handling to the bottom.
Closing checklist
- Register error-handling middleware before routing and dispatch middleware.
- Confirm
config/pipeline.phporder withgrep -n "->pipe(". - Verify the installed Mezzio version with
composer showbefore copying package names. - Test an error path with
curl -ior an automated test, not just the happy path. - Avoid mixing PSR-7 implementations across middleware and handlers.
Pipeline order is part of your application’s architecture. Check it the same way you would check a database migration or a routing table: explicitly, and before it reaches production.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.