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

查看:31
本文介绍了在 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

那么,如果我有 2 个以上的环境,我现在是否必须将它们全部设置在一个 .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

但似乎在最后几天默认检测环境已更改为:

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这里是本地环境,你可以改成production或者dev

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 从主 env 文件加载额外的 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天全站免登陆