$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to implement self defined request attributes in TYPO3 extensions - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to implement self defined request attributes in TYPO3 extensions - Stack Overflow

matteradmin14PV0评论

TYPO3 12 and later use request attributes to store internal informations. How does it work to add self defined attributes in TYPO3 extensions? I could not find anything in the docs.

Request Attributes

TYPO3/typo3_src-13.2.1/typo3/sysext/core/Classes/Http/ServerRequest.php:

namespace TYPO3\CMS\Core\Http;

class ServerRequest extends Request implements ServerRequestInterface
{
    protected array $attributes = [];

TYPO3 12 and later use request attributes to store internal informations. How does it work to add self defined attributes in TYPO3 extensions? I could not find anything in the docs.

Request Attributes

TYPO3/typo3_src-13.2.1/typo3/sysext/core/Classes/Http/ServerRequest.php:

namespace TYPO3\CMS\Core\Http;

class ServerRequest extends Request implements ServerRequestInterface
{
    protected array $attributes = [];
Share Improve this question asked Nov 15, 2024 at 19:40 Franz HolzingerFranz Holzinger 99811 silver badges24 bronze badges 1
  • This is a replacement for: Breaking: #102600 TSFE->applicationData removed – Franz Holzinger Commented Nov 20, 2024 at 17:05
Add a comment  | 

2 Answers 2

Reset to default 2

You can use a PSR-15 Middleware to achive that.

All you need is a Middleware class like:

Classes/Middleware/RequestEnrichingMiddleware.php

<?php

declare(strict_types=1);

namespace VENDOR\ExtName\Middleware;

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

class RequestEnrichingMiddleware implements MiddlewareInterface
{

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler,
    ): ResponseInterface {

        $myCustomDataArray = [
            'Foo' => 'Bar',
            'Foooo' => 'Baar',
        ];

        $request = $request->withAttribute('myCustomDataArray', $myCustomDataArray);

        return $handler->handle($request);
    }
}

And then you must register this middleware in: Configuration/RequestMiddlewares.php

<?php

return [
    'frontend' => [
        'vendor/my-middleware-identifier' => [
            'target' => \VENDOR\ExtName\Middleware\RequestEnrichingMiddleware::class,
            'after' => [
                'typo3/cms-core/response-propagation',
            ],
        ],
    ],
];

For more information see:
https://docs.typo3./m/typo3/reference-coreapi/main/en-us/ApiOverview/RequestLifeCycle/Middlewares.html#enriching-the-request

You can use withAttribute in PSR middlewares to enrich the request. This isn't really TYPO3 specific, but a general PSR approach. See:

https://docs.typo3./m/typo3/reference-coreapi/main/en-us/ApiOverview/RequestLifeCycle/Middlewares.html#enriching-the-request

You can set/get any attribute (don't use a name that TYPO3 already uses though).

Post a comment

comment list (0)

  1. No comments so far