Environment

The TYPO3 Core includes an environment class that contains all environment-specific information, mostly paths within the filesystem. This implementation replaces previously used global variables and constants like PATH_site that have been removed with TYPO3 v10.

The fully qualified class name is \TYPO3\CMS\Core\Core\Environment . The class provides static methods to access the necessary information.

To simulate environments in testing scenarios, the initialize()-method can be called to adjust the information.

Environment PHP API

getProjectPath()

The environment provides the path to the folder containing the composer.json. For projects without Composer setup, this is equal to getPublicPath().

getPublicPath()

The environment provides the path to the public web folder with index.php for the TYPO3 frontend. This was previously PATH_site. For projects without Composer setup, this is equal to getProjectPath().

getVarPath()

The environment provides the path to the var/ folder. This folder contains data like logs, sessions, locks, and cache files.

For Composer-based installations, it returns var/, in legacy installations typo3temp/var/.

use TYPO3\CMS\Core\Core\Environment;

// Composer-based installations: '/path/to/my-project/var/`
// Legacy installations: '/path/to/my-project/typo3temp/var/'
$pathToLabels = Environment::getVarPath();
Copied!

getConfigPath()

In Composer-based installation this method provides the path config/, in legacy installations typo3conf/.

The directory returned by this method contains the folders system/ containing the configuration files system/settings.php and system/additional.php and the folder sites/ containing the site configurations.

use TYPO3\CMS\Core\Core\Environment;

// Composer-based installations: '/path/to/my-project/config/system/settings.php`
// Legacy installations: '/path/to/my-project/typo3conf/system/settings.php'
$pathToSetting = Environment::getConfigPath() . 'system/settings.php';

// Composer-based installations: '/path/to/my-project/config/sites/mysite/config.yaml`
// Legacy installations: '/path/to/my-project/typo3conf/sites/mysite/config.yaml'
$pathToSiteConfig = Environment::getConfigPath() . 'sites/' . $siteKey . '/config.yaml';
Copied!

getLabelsPath()

The environment provides the path to var/labels/ in Composer-based installations, respective typo3conf/l10n/ folder in legacy installations. This folder contains downloaded translation files.

use TYPO3\CMS\Core\Core\Environment;

// Composer-based installations: '/path/to/my-project/var/labels/`
// Legacy installations: '/path/to/my-project/typo3conf/l10n/'
$pathToLabels = Environment::getLabelsPath();
Copied!

getCurrentScript()

Returns the path and filename to the current PHP script.

getContext()

Returns the current Application context, usually defined via the TYPO3_CONTEXT environment variable. May be one of Production, Testing, or Development with optional sub-contexts like Production/Staging.

Example, test for production context:

config/system/additional.php | typo3conf/system/additional.php
use TYPO3\CMS\Core\Core\Environment;

$applicationContext = Environment::getContext();
if ($applicationContext->isProduction()) {
   // do something only when in production context
}
Copied!