Laravel 5.2 Not Reading Env File

Laravel 5.2 not reading env file

If any of your .env variables contains white space, make sure you wrap them in double-quotes. For example:

SITE_NAME="My website"

Don't forget to clear your cache before testing:

php artisan config:cache
php artisan config:clear

Laravel not reading env variables from different env files

I'm adding this as an answer, but please note this isn't how .env file should be used according to laravel docs. It's just a way i needed to use due to some restrictions which required me to use different config files for each env, and load it in runtime. For correct usage of .env file check the docs.

This is a way of loading different config files in runtime depends on where the APP_ENV is pointing. I'm marking this as answered since it's answering this specific question.

.env

APP_ENV=specific_domain

.env.specific_domain

USE_SSL=true

Http/Kernel.php

public function __construct(Application $app, Router $router)
{
parent::__construct($app, $router);

$app_env = explode("=", file($app->environmentFilePath(), FILE_IGNORE_NEW_LINES)[0])[1];
$app->loadEnvironmentFrom(".env.$app_env");

}

Laravel ENV not reload

After completion of .env edit, You can clear the configuration cache with the following artisan command: php artisan config:cache

Use php artisan key:generate it will generate the new key to your .env file


NOTE: If there is still error then you do not need your restart computer just try this:

If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server

OR

If you have used XAMPP then restart your Apache server

Laravel 5.2 not reading env file

If any of your .env variables contains white space, make sure you wrap them in double-quotes. For example:

SITE_NAME="My website"

Don't forget to clear your cache before testing:

php artisan config:cache
php artisan config:clear

Laravel 5 - env() always returns null

env(...) function will not work after you cached the config. (starting from laravel 5.2 till current 5.7)

The Laravel documentation says

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

So the correct answer would be to

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

And I quoted it from the same documentation

But for a quick fix this will do:

php artisan config:clear

And now it should be clear why, when you tried config:cache, it did not help, even though it clears the config prior to caching.

Laravel 5.2 .env sometimes doesn't load in time

On the same project we recently started running some concurrent AJAX requests. The problem manifested 10 fold but at the same time it got me thinking from a different angle. I then Googled "Laravel multiple AJAX requests error" and found this: Laravel 5 losing sessions and .env configuration values in AJAX-intensive applications

Seeing this made me realise I have exactly the same problem. It's not the fact that i'm using the .env as a "configurable", it's actually 2 problems which are similar but have the same core issue: file read/lock access. My sessions were also acting up because we don't have DB access in our application so we have a heavy dependancy on storing and retrieving data from sessions variables (file system). Although im not sure the issue is related to lock files.

I'm using getenv() a lot throughout my application and a basic HTTP call can sometimes fail if an AJAX request is concurrently running as the .env lands up in a locked state i'm guessing.

My solution was to create a singleton that fires in my AppServiceProvider and store the .env data AND my session data in objects (memory). My theory was that once the file has been opened/closed that even if the request was still busy and another HTTP request came in the file would be closed already. From there on-wards I access all my .env variables and session data from the singleton. Now on the rare occasion the issue still appears.

Next issue that i'm sure will come up is when we have too many concurrent users loading the .env but i'll deal with that then.

Update

I decided to write my own script that opens and reads the contents of .env. Not a Single problem since... So as highlighted in my linked post, PHP dotenv by Lance Vucas seems to have issues on heavy AJAX driven projects. It may be a core PHP issue that is affecting this plugin.

I also never changed anything for my apparent session issues and since implementing my own script the session issue has also disappeared...



Related Topics



Leave a reply



Submit