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

时间:2022-02-21 10:05:15

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:

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

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文件,那么我可以忍受它。

3 个解决方案

#1


7  

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来解决它。创建项目时,此文件将作为存根添加到您的app文件夹,并用于覆盖配置值。

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

}

#2


5  

You dont have to use .env for everything. There are a few options.

你不必为所有事情使用.env。有几个选择。

Option 1 - Use only .env for a variable

选项1 - 仅对变量使用.env

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

Option 2 - Use only .env for 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 .env file 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配置文件夹选项不再可用。

#3


3  

It is easy to configure Laravel 5 environment.

配置Laravel 5环境很容易。

  1. Open your root application folder and find ".env.example",
  2. 打开根应用程序文件夹,找到“.env.example”,
  3. Copy and rename into ".env",
  4. 复制并重命名为“.env”,
  5. Please fit ".env" file into your environment,
  6. 请将“.env”文件放入您的环境中,
  7. If you use GIT, make sure you don't push this file to your GIT repository.
  8. 如果您使用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等云主机手动设置。

#1


7  

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来解决它。创建项目时,此文件将作为存根添加到您的app文件夹,并用于覆盖配置值。

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

}

#2


5  

You dont have to use .env for everything. There are a few options.

你不必为所有事情使用.env。有几个选择。

Option 1 - Use only .env for a variable

选项1 - 仅对变量使用.env

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

Option 2 - Use only .env for 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 .env file 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配置文件夹选项不再可用。

#3


3  

It is easy to configure Laravel 5 environment.

配置Laravel 5环境很容易。

  1. Open your root application folder and find ".env.example",
  2. 打开根应用程序文件夹,找到“.env.example”,
  3. Copy and rename into ".env",
  4. 复制并重命名为“.env”,
  5. Please fit ".env" file into your environment,
  6. 请将“.env”文件放入您的环境中,
  7. If you use GIT, make sure you don't push this file to your GIT repository.
  8. 如果您使用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等云主机手动设置。