2

I'm not satisfied by the Symfony Dotenv default behaviour, because I would like to have i.e. a .env.override which would override existing environment values.

So I would like to add the following code in config/bootstrap.php

$envLocal = dirname(__DIR__).'/.env.override';
if (file_exists($envLocal)) {
    (new Dotenv(false))->overload($envLocal);
}

Is it a problem to edit this file (config/bootstrap.php)? Can it be erased by future Symfony updates?

miken32
  • 42,008
  • 16
  • 111
  • 154
yolenoyer
  • 8,797
  • 2
  • 27
  • 61
  • 1
    tbh, I don't see the point. the .env should be a *local* file anyway! the defaults for the local file are in .env.dist and the inclusions of .env data is in config/**.yaml ... so technically, .env already is the override. do you now want an override of the override? – Jakumi Sep 17 '19 at 07:28
  • @Jakumi Thank you for your comment; actually, for reasons I won't explain here, we don't use `.env.dist` in our project (even if it would be a good idea). That's why my question is about modifying `config/bootstrap.php`. – yolenoyer Sep 17 '19 at 07:39
  • 1
    Depending on your version of the DotEnv component, the newer versions of DotEnv and the bootstrap will have the standard behavior of a `.env.local` to easily override env variables locally. Also on production you usually want to override ENV variables from nginx/apache, docker or as a server env var. Of course it's true, as the answer below says, that you can modify bootstrap.php, I don't really see why you would do this :) – Rein Baarsma Sep 17 '19 at 08:27
  • @ReinBaarsma Thank you, I agree it's not the right solution. It would be the goal of another question to go further about the problem, because actually, it may have something to deal with docker (.. or not, it's a bit mistery for now), because overriding is working well on production, but not in a docker env. Thank you all. – yolenoyer Sep 17 '19 at 08:37
  • @yolenoyer In our project we use a simple `.env` that contains everything needed for local development (which obviously has no production secrets whatsoever) and which simply lives in git. For production we have set environment variables in AWS through kubernetes and an encrypted config file, but it sounds like your production situation is already working. The cool thing about docker is that you don't really need a `.env.local` override, because all developers should have the same docker and therefore the same .env variables. – Rein Baarsma Sep 18 '19 at 12:30

1 Answers1

2

You can modify bootstrap.php to your heart's content.

The file is created when you run create-project symfony/skeleton, but it's part of your application files (it's not a dependency), so it's your responsibility to maintain it, and use it as you see fit.

Actually, if you upgraded your application to a version of Symfony that expected a different behaviour of bootstrap.php (or public/index.php, or (bin/console) you may need to update these files (among others).

yivi
  • 42,438
  • 18
  • 116
  • 138