Laravel 5 uploads env file based on .env.master?

I plan to do something similar by specifying which env file to load at application startup time by creating a variable in .env.master such as ENV_VER = dev.env

This is due to the fact that I have several branches, such as development, release-1.1, etc. Thus, by loading the env file based on the name specified in the main env file, developers no longer need to copy and paste the new variables into their local copy of .env and instead simply indicate which version of the env file to load in the main env file. By the way, I have several env files like dev.env, 1.6.env etc.

Is it possible?

+6
source share
2 answers

Definitely, I myself am inclined to β€œbend” the frames every time, and there is always a way, and not always a better solution. I am not giving a whole implementation here, just pointing at you in a certain direction, which might work for you.

You can extend the Laravel Illuminate\Foundation\Application base application class that contains the $environmentFile variable storage file that is loaded at boot time of the application, or possibly override the loadEnvironmentFrom($file) or environmentFile() function. All logic is up to you.

So basically all you have to do to β€œplay” with the .env download was ...

Create a new application class extending Laravel one:

 namespace MyNamespace; ... use Illuminate\Foundation\Application as App; /** * I'm doing alots of magic with .env in this class */ class Application extends App { ... /** * The environment file to load during bootstrapping. * * @var string */ protected $environmentFile = '.env.main'; ... } 

And now, since we have a new bootstraping class, we have to tell Laravel that we want to use it. Therefore, you will need to modify the bootstrap/app.php at the point where the new instance is created:

 ... $app = new MyNamespace\Application( realpath(__DIR__.'/../') ); ... 

Note. For inspiration, I recommend that you really look at the Illuminate\Foundation\Application class.

+6
source

In laravel 5. 5+, you can, maybe earlier, you can set the APP_ENV environment on your server or the server variable that the process can see (apache, command line ...)

this will allow you to use the suffix or file extension in your .env files to automatically download these files ...

  • APP_ENV = dev ::. Env.dev
  • APP_ENV = production ::. Env.production

much simpler than other solutions.

If you want to see how this is done, it starts with

1. application of bootstrapers

 protected $bootstrappers = [ \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Illuminate\Foundation\Bootstrap\LoadConfiguration::class, \Illuminate\Foundation\Bootstrap\HandleExceptions::class, \Illuminate\Foundation\Bootstrap\RegisterFacades::class, \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class, \Illuminate\Foundation\Bootstrap\RegisterProviders::class, \Illuminate\Foundation\Bootstrap\BootProviders::class, ]; 

2. LoadEnvironmentVariables

environment is determined first ...

\Illuminate\Foundation\Bootstrap\LoadConfiguration::bootstrap calls \Illuminate\Foundation\Bootstrap\LoadConfiguration::bootstrap \Illuminate\Foundation\Application::detectEnvironment

If

--env={env} for the CLI, then it will use this for APP_ENV.

yet

\Illuminate\Foundation\EnvironmentDetector::detectWebEnvironment called with a callback ...

 function () use ($config) { return $config->get('app.env', 'production'); } 

where app.env defaults to env('APP_ENV', 'production')

3 LoadConfiguration

\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::bootstrap calls ... \Illuminate\Foundation\Application::afterLoadingEnvironment which eventually ends up in

\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::checkForSpecificEnvironmentFile

which installs an environment file based on an env application if the file exists.

  $this->setEnvironmentFilePath( $app, $app->environmentFile().'.'.env('APP_ENV') ); 

letting him download .env.{APP_ENV} instead of .env

NOTE. Testing.

when running php unit tests. Illuminate / Foundation will attempt to download the .env.testing file for configurations!

+1
source

All Articles