Laravel 5配置-环境和覆盖 [英] Laravel 5 configuration - environments and overriding
问题描述
我安装了新的Laravel 5副本.
I installed fresh Laravel 5 copy.
我的detectEnvironment函数是这样定义的:
My detectEnvironment function is defined this way:
$app->detectEnvironment(function()
{
return 'local';
return getenv('APP_ENV') ?: 'production';
});
在config\local
中,我创建了database.php
文件:
<?php
return [
'nothing' => 'new',
];
我运行php artisan clear-compiled
.
我的WelcomeController
的index
方法是这样定义的:
My index
method of WelcomeController
is defined this way:
public function index(Application $app)
{
echo $app->environment();
var_dump($app['config']['database']);
//echo $app['config']['database'];
return view('welcome');
}
应用程序是通过以下方式导入的:use Illuminate\Foundation\Application;
Application was imported this way: use Illuminate\Foundation\Application;
我得到的结果是:
local array(1) { ["nothing"]=> string(3) "new" }
我希望Laravel将配置文件与生产版本1(使用默认的config\database.php
文件进行层叠).
whereas I would expect Laravel to cascade config file with production one (with the default config\database.php
file.
奇怪的是,即使我注释return 'local';
行再次运行php artisan clear-compiled
,它也会显示:
The strange thing is that even if I comment the line return 'local';
run again php artisan clear-compiled
it shows:
production array(1) { ["nothing"]=> string(3) "new" }
,因此似乎总是加载database.php
文件内容(此内容来自本地文件夹)并覆盖主database.php
文件.当我将此文件名更改为例如aaa.php
时,它再次正常工作.
so it seems it always loads database.php
file content (this one from local folder) and overrides main database.php
file. It works fine again when I change this file name to for example aaa.php
.
是错误还是环境配置不应该存储在config
目录中?但是,如果没有,应该将它们存储在哪里?我不知道这是错误还是功能,因此如果有人对此有更多了解,请给我一个提示.
Is it a bug or maybe environment configuration shouldn't be stored inside config
directory? 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.
推荐答案
尽管在Laravel dev(5.0)的文档中,有一些信息指出配置会级联,但并非如此.我已经在大约2周前对它进行了测试,目前看来,为环境提供不同值的唯一方法是使用ENV文件,在其中放置当前环境的自定义值.将设置放入目录无法正常使用,但是有可能会更改,或者在过去2周内已经更改.
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 5配置-环境和覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!