Laravel 5不会从点ENV文件中读取值 [英] Laravel 5 doesn't read values from dot ENV files

查看:504
本文介绍了Laravel 5不会从点ENV文件中读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个问题是否相关. LARAVEL 5仍处于开发阶段.在观看了有关LARAVEL 5的新功能的Laracast视频之一后,我退出了LARAVEL5.我忍不住等待它的正式发布.

I don't know if this question is relevant or not. LARAVEL 5 is still in developmental phase. I have pulled LARAVEL 5 after watching one of the Laracast video about new features in LARAVEL 5. I couldn't resist to wait for its formal release.

我将本地环境dot文件命名为.env.local.php.但是由于某些原因,在使用$_ENV['KEY']时,我无法从该点文件中获取值.

I named the local environment dot file as .env.local.php. But for some reason I am unable to get the the values from this dot file when using $_ENV['KEY'].

我非常确定我已经正确配置了环境.在执行$app->environment()时会显示正确的环境.是否在LARAVEL 5中更改了从点文件中获取值的方式,还是我错过了一些东西?

I am quite sure that I have configured the environment correctly. When doing $app->environment() shows the correct environment. Has it been changed in LARAVEL 5 the way we get the values from dot files or am I missing something ?

推荐答案

默认情况下,在environment.php文件中,您会看到以下内容:

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

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

,因此仅读取.env文件(注意,请注意.env,而不是.env.php-因此,您应该重命名文件-或根据需要添加为第二参数文件名.env.php).默认情况下,不会读取任何其他环境文件(.local.env)-您将需要手动加载它们.

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.

如果默认情况下没有这样的代码,则可能应该再次更新/安装Laravel 5(更改经常出现)

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

现在,我不知道您使用什么方法,但是您可以将您的环境名称也放入您的.env文件中,例如在APP_ENV变量中,创建具有所需内容的.local.env文件,然后您可以在environment.php文件中使用:

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');
    }

}

如果您不想这样做,则可以假定您使用基于PC的环境检测,然后根据$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.

如果不清楚,您还可以查看在Laravel 5中设置ENV变量的正确方法是什么?

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

这篇关于Laravel 5不会从点ENV文件中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆