如何为 dotenv 设置特定于环境的 .env 文件(在 Laravel 5 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28044322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:44:37  来源:igfitidea点击:

How to have environment specific .env files for dotenv (in Laravel 5)

phplaravelenvironment-variableslaravel-5

提问by Gnuffo1

I've just started using Laravel 5 which uses the dotenv library. This uses a .env file in the root of the project which sets the environment with this line:

我刚刚开始使用使用 dotenv 库的 Laravel 5。这在项目的根目录中使用了一个 .env 文件,该文件使用以下行设置环境:

APP_ENV=local

According to everything I've read on the subject, all other environmental specific configuration should be placed in this file, so database passwords, urls etc, which are then read into the main config array like this:

根据我读到的关于这个主题的所有内容,所有其他特定于环境的配置都应该放在这个文件中,所以数据库密码、urls 等,然后被读入主配置数组,如下所示:

env('DB_HOST', 'localhost')

While I feel this may work for a few specific things like database passwords that you might not want committed, what I really want is to be able to commit most or all of my different environmental values for each environment.

虽然我觉得这可能适用于一些您可能不想提交的特定内容,例如数据库密码,但我真正想要的是能够为每个环境提交大部分或所有不同的环境值。

Thus what I want is for .env to define APP_ENV as "local", "staging" or "production" and then have a .local.env or .env.local file containing the values, which I can then commit and the correct file will be loaded based on APP_ENV.

因此,我想要的是 .env 将 APP_ENV 定义为“本地”、“暂存”或“生产”,然后有一个包含值的 .local.env 或 .env.local 文件,然后我可以提交和正确的文件将基于 APP_ENV 加载。

Is this possible? Laravel 4 had the cascading config arrays which seemed a lot more flexible but if I can have an environmental .env file then I can live with that.

这可能吗?Laravel 4 有级联配置数组,这看起来更灵活,但如果我可以有一个环境 .env 文件,那么我可以接受它。

回答by Gnuffo1

Solved it in the end by modifying app/Providers/ConfigServiceProvider.php. This file is added as a stub to your app folder when you create a project and is intended for overriding config values.

最后通过修改app/Providers/ConfigServiceProvider.php解决了。当您创建项目时,此文件作为存根添加到您的应用程序文件夹中,用于覆盖配置值。

It now handles the cascading configs, so that any values in config/local/app.php for example will override config/app.php. As the comment below says it doesn't handle matching arrays in the environment config and will just replace then. But I can solve that when I need it.

它现在处理级联配置,例如 config/local/app.php 中的任何值都将覆盖 config/app.php。正如下面的评论所说,它不处理环境配置中的匹配数组,然后只会替换。但是我可以在需要时解决它。

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Symfony\Component\Finder\Finder;

class ConfigServiceProvider extends ServiceProvider {

    /**
     * Overwrite any vendor / package configuration.
     *
     * This service provider is intended to provide a convenient location for you
     * to overwrite any "vendor" or package configuration that you may want to
     * modify before the application handles the incoming request / command.
     *
     * Modified 2014-01-20 to allow environment specific configs to be loaded
     * from app/config/[environment]/ which will cascade over the base configs.
     *
     * @return void
     */
    public function register()
    {
        $config = app('config');
        $envPath = app()->configPath() . '/' . getenv('APP_ENV');

        foreach (Finder::create()->files()->name('*.php')->in($envPath) as $file)
        {
            $configName = basename($file->getRealPath(), '.php');
            $oldConfigValues = $config->get($configName);
            $newConfigValues = require $file->getRealPath();

            // Replace any matching values in the old config with the new ones.
            // Doesn't yet handle matching arrays in the config, it will just replace them.
            $config->set($configName, $newConfigValues + $oldConfigValues);
        }
    }

}

回答by Laurence

You dont have touse .envfor everything. There are a few options.

不必.env对所有事情都使用。有几个选项。

Option 1 - Use only .envfor a variable

选项 1 - 仅.env用于变量

'default' => env('DB_CONNECTION'),

Option 2 - Use only .envfor a variable, but have a system default if none exists

选项 2 - 仅.env用于变量,但如果不存在则使用系统默认值

'default' => env('DB_CONNECTION', 'mysql'),

Option 3 - just hard code your variable and not make it settable via the .env

选项 3 - 只对变量进行硬编码,而不是通过 .env

'default' => 'mysql',

Option 2 is probably the best for most config options. You still define (and commit) an option for your config to your git repo - but you can easily override it in any .envfile in the future if you want.

选项 2 可能是大多数配置选项的最佳选择。您仍然为您的 git 存储库定义(并提交)一个配置选项 - 但.env如果您愿意,您可以在将来的任何文件中轻松覆盖它。

Option 1 is best specifically for passwords, app keys etc - so they are never committed to your git repo.

选项 1 最适合密码、应用程序密钥等 - 因此它们永远不会提交到您的 git 存储库。

Option 3 is for a few config variables which you know will just never change.

选项 3 适用于一些您知道永远不会改变的配置变量。

Note- the cascading Laravel 4 config folder option is no longer available.

注意- 级联 Laravel 4 配置文件夹选项不再可用。

回答by user3851143

It is easy to configure Laravel 5 environment.

配置 Laravel 5 环境很容易。

  1. Open your root application folder and find ".env.example",
  2. Copy and rename into ".env",
  3. Please fit ".env" file into your environment,
  4. If you use GIT, make sure you don't push this file to your GIT repository.
  1. 打开您的根应用程序文件夹并找到“.env.example”,
  2. 复制并重命名为“.env”,
  3. 请将“.env”文件放入您的环境中,
  4. 如果您使用 GIT,请确保不要将此文件推送到您的 GIT 存储库。

For 'complete explanation', I write this configuration here.

为了“完整的解释”,我在这里写了这个配置。

I quote from the dotenv developer;

我引用了 dotenv 开发者的话;

phpdotenv is made for development environments, and generally should not be used in production. In production, the actual environment variables should be set so that there is no overhead of loading the .env file on each request. This can be achieved via an automated deployment process with tools like Vagrant, chef, or Puppet, or can be set manually with cloud hosts like Pagodabox and Heroku.

phpdotenv 是为开发环境制作的,一般不应用于生产环境。在生产中,应该设置实际的环境变量,以便在每个请求上加载 .env 文件没有开销。这可以通过使用 Vagrant、Chef 或 Puppet 等工具的自动化部署过程来实现,也可以使用 Pagodabox 和 Heroku 等云主机手动设置。