RSS Amplifier

susi.dev · Nov 18, 2021

PSR-15 Middleware: Add custom header in TYPO3

0
Sign in to vote or save

Susanne Moog · susi.dev

Published on

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.

Read the original on susi.dev

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.