Im nachfolgenden Beispiel wird eine Middleware vor "typo3/cms-frontend/page-resolver" platziert. An dieser Stelle ist jedoch noch kein TSFE vorhanden ("typo3/cms-frontend/tsfe" bzw. "typo3/cms-frontend/prepare-tsfe-rendering" werden erst später ausgeführt). Die TypoScript Konfiguration kann in diesem Falle dennoch manuell mithilfe der Klassen "RootlineUtility" und "TemplateService"geladen werden, sofern die Middleware nach "typo3/cms-frontend/site" ausgeführt wird.

Configuration\RequestMiddlewares.php

<?php

return [
    'frontend' => [
        'vendor/extension/earlymiddleware' => [
            'target' => Vendor\Extension\Middleware\EarlyMiddleware::class,
            'after' => [
                'typo3/cms-frontend/site'
            ],
            'before' => [
                'typo3/cms-frontend/backend-user-authentication'
            ],
        ],
    ],
];

Middleware\EarlyMiddleware.php

<?php
namespace Vendor\Extension\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\RootlineUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * EarlyMiddleware
 */
class EarlyMiddleware implements MiddlewareInterface
{

    /**
     * Hook to preprocess the current request
     *
     * @param ServerRequestInterface $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     */
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {

        if(strpos(strtolower($request->getUri()->getPath()), '/earlymiddleware') === 0) {
            /** @var Site $site */
            $site = $request->getAttribute('site');
            /** @var RootlineUtility $rootlineUtility */
            $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $site->getRootPageId());
            $rootline = $rootlineUtility->get();
            /** @var TemplateService $templateService */
            $templateService = GeneralUtility::makeInstance(TemplateService::class);
            $templateService->tt_track = 0;
            $templateService->runThroughTemplates($rootline);
            $templateService->generateConfig();
            // Now setup and constants are available like this:
            //$templateService->setup['module.']['tx_extension.']['settings.']['foo'];
            //$templateService->setup_constants['module.']['tx_extension.']['settings.']['foo'];
            // Do anything...
        }

        // Proceed further middlewares
        return $handler->handle($request);
    }

}




Kommentare