Laravel 5配置-环境和覆盖 [英] Laravel 5 configuration - environments and overriding

查看:190
本文介绍了Laravel 5配置-环境和覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了新的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.

我的WelcomeControllerindex方法是这样定义的:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆