Laravel 5: .env APP_ENV not Being Read Properly and Quick Fix

So I encountered an issue with Laravel 5 where your APP_ENV in the .env file might not be read. Even if you change it through the Homstead.yaml file, change /etc/php5/fpm/php-fpm.conf, change your .profile file, etc. you still may not see the variable properly reflected when doing something like var_dump($_SERVER) or examining the Application::Environment() function. This post discusses what the issue is and a quick fix for this problem.

The problem is that the .env file gets read by Dotenv.php, which somehow sees the variable APP_ENV set some place (not sure where exactly) and prevents it from being overridden. This occurs under this line:

if (static::$immutable === true && !is_null(static::findEnvironmentVariable($name))) {

Because static::$immutable is true by default and that the environmental variable is set some place outside of the .env file, your APP_ENV setting gets ignored.

Now, a very hacky way of fixing the issue is to set the $immutable variable to true so that your .env variables (if somehow get encountered elsewhere) override the main settings. The way I got around this issue is through modifying my bootstrap/autoload.php file.

I pretty much placed this under the autoload.php require line like this:

require __DIR__.'/../vendor/autoload.php';


This will fix that particular issue. Is it the correct way to go? Don’t know. I’m not exactly sure where the $_SERVER[‘APP_ENV’] setting is getting set since I’ve searched everywhere. But at least this will fix this issue.

(Visited 538 times, 1 visits today)