dotenv-connector within TYPO3 CMS
Asked Answered
D

3

7

I try to use helhum/dotenv-connector in my TYPO3 Project.

I have done the following:

my composer.json:

{
    "require": {
        "typo3/cms": "^8.5",
        "helhum/dotenv-connector": "1.0.0",
        "helhum/typo3-console": "^4.1"
    },
    "extra": {
        "helhum/typo3-console": {
            "install-extension-dummy": false
        },
        "typo3/cms": {
            "cms-package-dir": "{$vendor-dir}/typo3/cms",
            "web-dir": "web"
        },
        "helhum/dotenv-connector": {
            "env-dir": "",
            "allow-overrides": true,
            "cache-dir": "var/cache"
        }
    }
}

Then I ran

composer install

After that I setup the TYPO3 using the command

php vendor/bin/typo3cms install:setup

This should be similar with doing the install the "normal" way.

After that, i placed a .env next to my composer.json

This .env contains the following:

TYPO3_CONTEXT="Development"
TYPO3__DB__database="dotenvconnector"
TYPO3__DB__host="127.0.0.1"
TYPO3__DB__password="root"
TYPO3__DB__port="3306"
TYPO3__DB__username="root"

Then i removed all informations about the DB from web/typo3conf/LocalConfiguration.php using the typo3_console-command

php vendor/bin/typo3cms configuration:remove DB

I then ran composer install and composer update again.

When calling the TYPO3 in the browser now, it keeps telling me

The requested database connection named "Default" has not been configured.

So what am i missing? Obviously my .env is not parsed or used at all.

FYI: Cachefile is written in var/cache with the following content:

<?php
putenv('TYPO3__DB__database=dotenvconnector');
$_ENV['TYPO3__DB__database'] = 'dotenvconnector';
$_SERVER['TYPO3__DB__database'] = 'dotenvconnector';
putenv('TYPO3__DB__host=localhost');
$_ENV['TYPO3__DB__host'] = 'localhost';
$_SERVER['TYPO3__DB__host'] = 'localhost';
putenv('TYPO3__DB__password=root');
$_ENV['TYPO3__DB__password'] = 'root';
$_SERVER['TYPO3__DB__password'] = 'root';
putenv('TYPO3__DB__port=3306');
$_ENV['TYPO3__DB__port'] = '3306';
$_SERVER['TYPO3__DB__port'] = '3306';
putenv('TYPO3__DB__username=root');
$_ENV['TYPO3__DB__username'] = 'root';
$_SERVER['TYPO3__DB__username'] = 'root';
Draggle answered 6/1, 2017 at 6:39 Comment(1)
problem also there in TYPO3 7.6.*Draggle
B
7

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.

Bandung answered 6/1, 2017 at 7:56 Comment(4)
Do you use the dot-env-connector from helhum? Or is this another approach? As you can see here : github.com/helhum/TYPO3-Distribution/blob/master/.env-example you can use this: Set arbitrary TYPO3_CONF_VARS values, following the convention: TYPO3__<section>[__<sub-section>]__property So in his distribution it works out of the box and i dont get what i am missing. Will try your approach as well. Thx for the answer.Draggle
@Draggle It is the same approach (parsing .env-files and overwriting TYPO3 settings), but not using the »helhum/dotenv-connector«.Galvanic
To clarify: Require »helhum/dotenv-connector«, which reads the .env file even before starting TYPO3 & passes the values to the enviroment. These values need to be read and assigned to TYPO3 manually, using getenv(), $_ENV or $_SERVER. The connector doesnt overwrite settings automatically. You may do this in the AdditionalConfiguration.php eg. $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['host'] = getenv('TYPO3__DB__host');. The »helhum/TYPO3-Distribution« also contains the package »helhum/config-loader« which merges TYPO3 settings and the .env-values automatically.Galvanic
@mathias-schreiber is 'DB' => [ 'Connections' => [ 'Default' => [ ... valid syntax for LocalConfirguration.php ?Swabber
N
3

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().

Nims answered 9/1, 2017 at 8:27 Comment(0)
B
1

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'

Burette answered 26/9, 2018 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.