Published on

PSR-15 Middleware: Add custom header in TYPO3

Authors

TYPO3 uses PSR‑15 for its HTTP middleware stack. Here’s a tiny example that adds a custom header.

Middleware class

namespace Vendor\MyExt\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class AddHeaderMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $response = $handler->handle($request);
        return $response->withHeader('X-App', 'MyExt');
    }
}

Registration

Configuration/RequestMiddlewares.php:

<?php
return [
  'frontend' => [
    'vendor/my-ext/add-header' => [
      'target' => Vendor\MyExt\Middleware\AddHeaderMiddleware::class,
      'after' => ['typo3/cms-frontend/base-redirect-resolver'],
      'before' => ['typo3/cms-frontend/content-length-handler'],
    ],
  ],
];

Tip

Keep middlewares focused and fast; leverage caching where appropriate and place them at the right spot in the chain.