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!