Laravel 5 doesn't read values from dot ENV files

不羁岁月 提交于 2019-12-02 07:41:46
Marcin Nabiałek

By default in environment.php file you have something like that:

if (file_exists(__DIR__.'/../.env'))
{
    Dotenv::load(__DIR__.'/../');
}

so only .env file is being read (notice .env not .env.php - so you should rename your file - or you can add as 2nd parameter file name .env.php if you want). Any other environment files (.local.env) are not being read by default - you will need to load them manually.

If you don't have such code by default, you should probably update/install Laravel 5 again (changes appear very often)

Now, I don't know what method you use, but you can put in your .env file also your environment name in for example APP_ENV variable, create .local.env file with content you want and then you could use in environment.php file:

if (file_exists(__DIR__.'/../.env'))
{
    Dotenv::load(__DIR__.'/../');

    if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
        echo "loading";
        Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
    }

}

If you don't want to do it this way, you can probably change the other and load env file you want based on $env assuming you use PC based environment detection.

If it's unclear you can also look at What's the correct way to set ENV variables in Laravel 5?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!