I'm trying to add an HTTP header to error pages (404, 403, etc.) using middleware.
(new Extend\Middleware('forum'))->add(ExampleMiddleware::class)
However, it seems that everything gets overwritten on error pages and the headers I add disappear. Additionally, when I dump the response on these pages, I see a 200 status code.
class ExampleMiddleware implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
$response = $handler->handle($request);
$response = $response->withHeader('X-Test', 'test');
dump($response); // Shows status 200 even on 404 pages
return $response;
}
}
This suggests that error pages are created after all middlewares have executed and override what was done in the middleware.
How can I add an HTTP header to error page responses?