dotenv-connector within TYPO3 CMS

夙愿已清 提交于 2019-12-03 03:35:13
Mathias Schreiber

Our setups work like this:

AdditionalConfiguration.php

$loader = new Dotenv\Dotenv(__DIR__ . '/../../', '.env.defaults');
$loader->load();
$loader = new Dotenv\Dotenv(__DIR__ . '/../../');
$loader->overload();

Interesting to see here that we run with a .env.defaults file that holds the standard config (no users or passwords of course) which we then overload with the custom .env file per user/environment. This helps a lot when adding new functionality which requires a new .env configuration so other people on the team don't run into Fatals or Exceptions.

$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname'] = getenv('TYPO3_DB_NAME');
$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['host'] = getenv('TYPO3_DB_HOST');
$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['password'] = getenv('TYPO3_DB_PASSWORD');
$GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['user'] = getenv('TYPO3_DB_USER');

LocalConfiguration.php

return [
'BE' => [
    'debug' => '<set by dotenv>',
    'explicitADmode' => 'explicitAllow',
    'installToolPassword' => '<set by dotenv>',
    'loginSecurityLevel' => 'rsa',
    'sessionTimeout' => '<set by dotenv>',
],
'DB' => [
    'Connections' => [
        'Default' => [
            'charset' => 'utf8',
            'dbname' => '<set by dotenv>',
            'driver' => 'mysqli',
            'host' => '<set by dotenv>',
            'password' => '<set by dotenv>',
            'port' => 3306,
            'user' => '<set by dotenv>',
        ],
    ],
]...

I didn't paste the entire config but I think you get the point.

The dotenv-connector reads the .env file into the environment, but does not assign any values to TYPO3 configuration variables. You should be able to read them with getenv in your php code. The connector is not specifically geared towards TYPO3, but is a general tool for any composer based php application. Therefore it would be out of the scope of the project, to know about the TYPO3 specific variable assignments.

There is another project, the configuration loader, that can help to assign environment variables to TYPO3 configuration variables.

.env -dotenv-connector-> environment -configuration-loader-> $GLOBALS['TYPO3_CONF_VARS']

The configuration loader can be found at https://github.com/helhum/config-loader . And an example of it all wired together in https://github.com/helhum/TYPO3-Distribution .

You don't have to use the configuration loader. You could also assign the values manually with getenv().

One important note with PHP 7.2 (on TYPO3 v9) and the usage of argon hash: You must use single quotes / ticks for the values in the .env file. Example: Instead of my_value="foobar" write my_value='foobar'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!