TYPO3 request object
The TYPO3 request object is an implementation of the PSR-7 based
\Psr\
containing TYPO3-specific attributes.
See also
Getting the PSR-7 request object
The PSR-7 based request object is available in most contexts. In some scenarios, like PSR-15 middlewares or backend module controllers, the PSR-7 base request object is given as argument to the called method.
Extbase controller
New in version 11.3
The request object compatible with the PSR-7
\Psr\
is available in an
Extbase controller via the class property
$this->request
:
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
final class MyController extends ActionController
{
// ...
public function myAction(): ResponseInterface
{
// ...
// Retrieve the language attribute via the request object
$language = $this->request->getAttribute('language');
// ...
}
}
Note
Prior to TYPO3 v11.3, a custom Extbase request object is available that does not adhere to the PSR-7 standard. If you want to stay compatible with TYPO3 v10 and TYPO3 v11 you have to use the global variable.
ViewHelper
In a ViewHelper you can get the rendering request from the rendering context.
For this you have to rely on the fact that a
\TYPO3\
is passed to the
ViewHelpers render
method, even though it is declared as
Rendering
, which does not have the method:
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\ViewHelpers;
use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
final class MyViewHelper extends AbstractViewHelper
{
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
{
/** @var RenderingContext $renderingContext */
$request = $renderingContext->getRequest();
}
}
Note, that Rendering
is internal and subject to
change in future versions of TYPO3.
User function
In a TypoScript user function the request object is available as third parameter of the called class method:
use Psr\Http\Message\ServerRequestInterface;
final class MyUserFunction
{
public function doSomething(
string $content,
array $conf,
ServerRequestInterface $request
): string {
// ...
// Retrieve the language attribute via the request object
$language = $request->getAttribute('language');
// ...
}
}
Data processor
A data processor receives a
reference to the \TYPO3\
as first argument for the process
method. This object provides a
get
method:
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;
final class MyProcessor implements DataProcessorInterface
{
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
): array {
$request = $cObj->getRequest();
// ...
}
}
Last resort: global variable
TYPO3 provides the request object also in the global variable
$GLOBALS
. Whenever it is possible the request should be
retrieved within the contexts described above. But this is not always possible
by now.
When using the global variable, it should be wrapped into a getter method:
// use Psr\Http\Message\ServerRequestInterface;
private function getRequest(): ServerRequestInterface
{
return $GLOBALS['TYPO3_REQUEST'];
}
This way, it is only referenced once. It can be cleaned up later easily when the request object is made available in that context in a future TYPO3 version.
Attributes
Attributes enriches the request with further information. TYPO3 provides attributes which can be used in custom implementations.
The attributes can be retrieved via
// Get all available attributes
$allAttributes = $request->getAttributes();
// Get only a specific attribute, here the site entity in frontend context
$site = $request->getAttribute('site');
The request object is also available as a global variable in
$GLOBALS
. This is a workaround for the Core which has to
access the server parameters at places where $request
is not available.
So, while this object is globally available during any HTTP request, it is
considered bad practice to use this global object, if the request is accessible
in another, official way. The global object is scheduled to vanish at a later
point once the code has been refactored enough to not rely on it anymore.
The following attributes are available in frontend context:
- Application type
- Current content object
- Frontend controller
- Frontend TypoScript
- Frontend user
- Language
- Nonce
- Normalized parameters
- Routing
- Site
The following attributes are available in backend context: