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_
that have been removed with TYPO3 v10.
The fully qualified class name is \TYPO3\
. 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
Tip
A comprehensive list of methods can be found in the Class Reference.
getProjectPath()
The environment provides the path to the folder containing the composer.
.
For projects without Composer setup, this is equal to getPublicPath().
getPublicPath()
The environment provides the path to the public web folder with
index.
for the TYPO3 frontend. This was previously PATH_
.
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();
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/
and system/
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';
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();
getCurrentScript()
Returns the path and filename to the current PHP script.
getContext()
Returns the current Application context, usually defined via the TYPO3_
environment variable.
May be one of Production
, Testing
, or Development
with optional sub-contexts like Production/
.
Example, test for production context:
use TYPO3\CMS\Core\Core\Environment;
$applicationContext = Environment::getContext();
if ($applicationContext->isProduction()) {
// do something only when in production context
}