Laravel 5 配置 - 环境和覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27343021/
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:31:58  来源:igfitidea点击:

Laravel 5 configuration - environments and overriding

phplaravellaravel-5laravel-environment

提问by Marcin Nabia?ek

I installed fresh Laravel 5 copy.

我安装了新的 Laravel 5 副本。

My detectEnvironment function is defined this way:

我的 detectEnvironment 函数是这样定义的:

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

In config\localI've created database.phpfile:

config\local我创建的database.php文件中:

<?php

return [
    'nothing' => 'new',
];

I run php artisan clear-compiled.

我跑php artisan clear-compiled

My indexmethod of WelcomeControlleris defined this way:

我的index方法WelcomeController是这样定义的:

public function index(Application $app)
{
    echo $app->environment();
    var_dump($app['config']['database']);
    //echo $app['config']['database'];
    return view('welcome');
}

Application was imported this way: use Illuminate\Foundation\Application;

应用程序是这样导入的: use Illuminate\Foundation\Application;

The result I get is:

我得到的结果是:

local array(1) { ["nothing"]=> string(3) "new" } 

whereas I would expect Laravel to cascade config file with production one (with the default config\database.phpfile.

而我希望 Laravel 将配置文件与生产配置文件(使用默认config\database.php文件。

The strange thing is that even if I comment the line return 'local';run again php artisan clear-compiledit shows:

奇怪的是,即使我return 'local';再次评论该行,php artisan clear-compiled它也会显示:

production array(1) { ["nothing"]=> string(3) "new" } 

so it seems it always loads database.phpfile content (this one from local folder) and overrides main database.phpfile. It works fine again when I change this file name to for example aaa.php.

所以它似乎总是加载database.php文件内容(这个来自本地文件夹)并覆盖主database.php文件。当我将此文件名更改为例如aaa.php.

Is it a bug or maybe environment configuration shouldn't be stored inside configdirectory? But if not, where should they be store? I don't know if it's a bug or a feature so if anyone knows more about it, please give me a clue.

这是一个错误还是环境配置不应该存储在config目录中?但如果没有,它们应该存放在哪里?我不知道这是一个错误还是一个功能,所以如果有人知道更多,请给我一个线索。

回答by Marcin Nabia?ek

Although in documentation for Laravel dev (5.0) there is info that configuration will cascade it's not true. I have tested it about 2 weeks ago and it seems at the moment the only way to have different values for environments is using ENV file where you put custom values for current environment. Putting settings in directories won't work as it used to work however it's possible it will change or maybe has been already changed for last 2 weeks.

尽管在 Laravel dev (5.0) 的文档中有关于配置会级联的信息,但事实并非如此。我在大约 2 周前对其进行了测试,目前看来,为环境设置不同值的唯一方法是使用 ENV 文件,您可以在其中为当前环境放置自定义值。将设置放在目录中将无法像以前那样工作,但是它可能会更改,或者可能已经更改了过去 2 周。

回答by An Phan

There's a packagethat brings the cascading config system back to Laravel 5.

有一个可以将级联配置系统带回 Laravel 5。

Disclaimer: I am the author.

免责声明:我是作者。

回答by Margus Pala

For me it looks like defect in Laravel 5 dev branch. I was able to work around by adding manual environment detection and configuration. This code does it.

对我来说,它看起来像是 Laravel 5 dev 分支中的缺陷。我能够通过添加手动环境检测和配置来解决。这段代码做到了。

'default' => $app->environment()=='testing'?'sqlite':'mysql',

回答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. For 'complete explanation', I write this configuration here.
  1. 打开您的根应用程序文件夹并找到“.env.example”,
  2. 复制并重命名为“.env”,
  3. 请将“.env”文件放入您的环境中,
  4. 如果您使用 GIT,请确保不要将此文件推送到您的 GIT 存储库。为了“完整的解释”,我在这里写了这个配置。

Edited;

已编辑;

I quote from the developer in His github repository readme.md file;

我在他的 github 存储库 readme.md 文件中引用了开发人员;

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 等云主机手动设置。

So, you need to create ".env" file per machine and don't use ".env" file in your production server.

因此,您需要为每台机器创建“.env”文件,并且不要在您的生产服务器中使用“.env”文件。