Why is constants.php loaded before .env

In Pimcore 5.8 we were using environment variables to set constants. That made our live a lot easier in regards of deploying the application.

This worked fine as constants were loaded after the environent variables were loaded.

    // load .env file if available
    $dotEnvFile = PIMCORE_PROJECT_ROOT . '/.env';
    if (file_exists($dotEnvFile)) {
        (new Dotenv())->load($dotEnvFile);
    }

    // load custom constants
    $customConstantsFile = PIMCORE_PROJECT_ROOT . '/app/constants.php';
    if (file_exists($customConstantsFile)) {
        include_once $customConstantsFile;
    }

In pimcore 6, it seems that for some reason that this has been turned around:

      // load custom constants
    $customConstantsFile = PIMCORE_PROJECT_ROOT . '/app/constants.php';
    if (file_exists($customConstantsFile)) {
        include_once $customConstantsFile;
    }

    self::prepareEnvVariables();

Now, is there any good reason for this change? Otherwise I would gladly create a pull request to fix this.

1 Like

I’ve tracked the commit that changed this, but don’t really see how the addition of switching constants and envs helps with the issue the commit references:

f0abc8c5ca78216d1caf228adac337b6dbc9617a

I agree with @davidmaes. There should be a possibility to define constants after the .env variables are loaded because this way you can - guess what - define constants depending on what is configured in the environment.

I have proposed a merge request for at least adding a second constants file after the loading of .env variables but it was rejected as “unecessary” :frowning_face:

They changed the order because they need the value of certain constants within self::prepareEnvVariables() but still there should be a possibility to also define constants afterwards.