在Laravel 5中设置ENV变量的正确方法是什么? [英] What's the correct way to set ENV variables in Laravel 5?

查看:235
本文介绍了在Laravel 5中设置ENV变量的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在laravel 4中,我们有:

In laravel 4 we had:

$env = $app->detectEnvironment(array(
    'local' => array('homestead')
));

默认情况下.

但是在laravel 5中,它已更改为:

But in laravel 5 it's changed to:

$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'production';
});

此外,他们还排除了.gitignore中的 .env.* 行,现在它具有:

Also, they have excluded .env.* line in .gitignore, now it has:

.env

并添加了文件.env.example:

And added file .env.example:

APP_ENV=local
APP_KEY=SomeRandomString
DB_USERNAME=homestead
DB_PASSWORD=homestead

那么,如果我有两个以上的环境,我现在是否必须将所有环境都设置在一个.env文件中?例如:

So, if i have more than 2 environments, do i have to set all of them in a single .env file now? E.g.:

APP_ENV=local
DB_PASSWORD=123

APP_ENV=alpha
DB_PASSWORD=456

如果我没有.env文件,laravel怎么会知道我正在使用什么环境?

If i would have no .env file, how laravel will know what environment i am using?

推荐答案

您可以执行与Laravel 4中完全相同的操作:

You can do it exactly the same as in Laravel 4:

$env = $app->detectEnvironment(array(
    'local' => array('homestead')
));

*.env文件仅用于放置不应放入VCS的敏感数据. Laravel 4中也是如此

*.env file are just used to put sensitive data that shouldn't be put into VCS. The same is in Laravel 4

但是似乎最近几天默认的detectEnvironment更改为:

but is seems that in last days default detectEnvironment was changed to:

$env = $app->detectEnvironment(function()
{
    return getenv('APP_ENV') ?: 'production';
});

因此您可以使用PC名称或ENV文件中的设置变量.

so you can use either setting variable from PC name or from ENV file.

如果您在主env文件中使用基于ENV的环境检测(默认情况下为.env文件,则需要添加:

If you use ENV based environment detection in main env file (by default .env file you need to add:

APP_ENV=local

当然local这是本地环境,您可以将其更改为productiondev

Of course local here is local environment, you can change it into production or dev

目前,我所看到的最重要的问题是,在生产时需要记住将.env文件内容从APP_ENV=local更改为APP_ENV=production的原因,所以我认为更好的方法是旧的默认方法基于PC名称.

At the moment the most important issue I see is that you need to remember when going on production to change this .env file content from APP_ENV=local to APP_ENV=production so in my opinion much better method is the old default method based on PC names.

现在有ENV文件.如果您使用基于ENV的环境检测,则应仅将其放入ENV文件中:

Now ENV files. If you use ENV based environment detection, you should put into your ENV file only:

APP_ENV=local

现在,您可以为不同的环境创建单独的ENV文件,例如:

Now you can create separate ENV files for your different environments for example:

.local.env :

 MY_DB=testdb

.production.env :

MY_DB=productiondb

,现在可以在bootstrap.environment.php文件中进行修改:

and now in bootstrap.environment.php file you can modfiy:

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

进入:

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

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

从主环境文件中加载基于APP_ENV的其他环境文件.

to load extra env file based on APP_ENV from main env file.

现在您将能够像往常一样在其他配置文件中使用它:$_ENV['MY_DB']

Now you will be able to use it in your other configuration file as always: $_ENV['MY_DB']

这篇关于在Laravel 5中设置ENV变量的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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