Skip to main content

Laravel 5.4 .env files — how they autoload

Laravel 5.4 .env files — how they autoload

Laravel 5.4 .env files How do I load the .env.production file vs .env.dev file or the base .env file?


In my vagrant box I had ...
    setenv:
        - 'APP_ENV dev'
After creating a .env.dev , I noticed it was not loading that file!
WHY? HOW!?
Well, the bootstrap is auto detecting the app environment from the servers APP_ENV environment variable which can be set on the server or virtual host level of your server.
The code that does it... vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/LoadEnvironmentVariables.php
/**
 * Detect if a custom environment file matching the APP_ENV exists.
 *
 * @param  \Illuminate\Contracts\Foundation\Application  $app
 * @return void
 */
protected function checkForSpecificEnvironmentFile($app)
{
    if (php_sapi_name() == 'cli' && with($input = new ArgvInput)->hasParameterOption('--env')) {
        $this->setEnvironmentFilePath(
            $app, $app->environmentFile().'.'.$input->getParameterOption('--env')
        );
    }

    if (! env('APP_ENV')) {
        return;
    }

    // this will load the .env.{APP_ENV} file
    $this->setEnvironmentFilePath(
        $app, $app->environmentFile().'.'.env('APP_ENV')
    );
}

Popular Posts