TSFE

What is TSFE?

TSFE is short for \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController , a class which exists in the system extension EXT:frontend.

As the name implies: A responsibility of TSFE is page rendering. It also handles reading from and writing to the page cache. For more details it is best to look into the source code.

There are several contexts in which the term TSFE is used:

  • PHP: It is passed as request attribute frontend.controller
  • PHP: It was and is available as global array $GLOBALS['TSFE'] in PHP.
  • TypoScript: TypoScript function TSFE which can be used to access public properties in TSFE.

The TypoScript part is covered in the TypoScript Reference: TSFE. In this section we focus on the PHP part and give an overview, in which way the TSFE class can be used.

Accessing TSFE

From the source:

When calling a frontend page, an instance of this object is available as $GLOBALS['TSFE'] , even though the Core development strives to get rid of this in the future.

If access to the \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController instance is necessary, use the request attribute frontend.controller:

$frontendController = $request->getAttribute('frontend.controller');
Copied!

TSFE is not available in all contexts. In particular, it is only available in frontend contexts, not in the backend or the command line.

Initializing $GLOBALS['TSFE'] in the backend is sometimes done in code examples found online. This is not recommended. TSFE is not initialized in the backend context by the Core (and there is usually no need to do this).

From the PHP documentation:

As of PHP 8.1.0, $GLOBALS is now a read-only copy of the global symbol table. That is, global variables cannot be modified via its copy.

-- https://www.php.net/manual/en/reserved.variables.globals.php

Howtos

Following are some examples which use TSFE and alternatives to using TSFE, where available:

Access ContentObjectRenderer

Access the \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer (often referred to as "cObj"):

// !!! discouraged
$cObj = $GLOBALS['TSFE']->cObj;
Copied!

Obtain TSFE from the request attribute frontend.controller:

$frontendController = $request->getAttribute('frontend.controller');
$cObj = $frontendController->cObj;
Copied!

In the case of the USER content object (for example, a non-Extbase plugin) use setter injection:

EXT:my_extension/Classes/UserFunctions/MyClass.php
<?php

declare(strict_types=1);

namespace MyVendor\MyExtension\UserFunctions;

use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

final class MyClass
{
    /**
     * Reference to the parent (calling) cObject set from TypoScript
     */
    private ContentObjectRenderer $cObj;

    public function setContentObjectRenderer(ContentObjectRenderer $cObj): void
    {
        $this->cObj = $cObj;
    }

    // ... other methods
}
Copied!

Access current page ID

Access the current page ID:

// !!! discouraged
$pageId = $GLOBALS['TSFE']->id;
Copied!

Can be done using the routing request attribute:

$pageArguments = $request->getAttribute('routing');
$pageId = $pageArguments->getPageId();
Copied!

Access frontend user information

// !!! discouraged
$feUser = $GLOBALS['TSFE']->fe_user;
Copied!

Use the frontend.user request attribute:

/** @var \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication $frontendUser */
$frontendUser = $request->getAttribute('frontend.user');
Copied!

Some information via frontend and backend users con be obtained via the user aspect. For example:

// return whether a frontend user is logged in
$context->getPropertyFromAspect('frontend.user', 'isLoggedIn');
Copied!

Get current base URL

It used to be possible to get the base URL configuration (from TypoScript config.baseURL) with the TSFE baseURL property. The property is now protected and deprecated since TYPO3 v12. Already in earlier version, site configuration should be used to get the base URL of the current site.

// !!! deprecated
$GLOBALS['TSFE']->baseURL
Copied!
/** @var \TYPO3\CMS\Core\Site\Entity\Site $site */
$site = $request->getAttribute('site');
/** @var array $siteConfiguration */
$siteConfiguration = $site->getConfiguration();
$baseUrl = $siteConfiguration['base'];
Copied!