如何在 Laravel-4 中为不同环境包含自定义配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17671781/
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
How to include custom config for different environment in Laravel-4
提问by Ironwind
$env = $app->detectEnvironment(array(
'local' => array('*.local'),
));
The original config files can be used in my local environment like the database.php
, cache.php
etc. But my custom config isn't getting used in my local environment.
原来的配置文件可以在我的本地环境下,如使用database.php
,cache.php
等等。但是我的自定义配置是不是在我的本地环境的习惯。
Is there a way to add my custom config on it?
有没有办法在上面添加我的自定义配置?
回答by Andreyco
First, you need to define, what is 'local' environment
首先,您需要定义什么是“本地”环境
$env = $app->detectEnvironment(array(
// everything that contains 'localhost' or '127.0.0.1'
// in URL will be considered to run in local env.
'local' => array('localhost', '127.0.0.1'),
));
Then, you need to create directory named local
in app/config
. If you want to override DB settings in local env. create the file: app/config/local/database.php
and override setting in this file, for example:
然后,你需要创建一个名为目录local
在app/config
。如果要覆盖本地环境中的数据库设置。创建文件:app/config/local/database.php
并覆盖此文件中的设置,例如:
'connections' => array(
'mysql' => array(
'username' => 'otherUserName',
'password' => 'otherPassword',
),
),
In this file, you do not need to specify all options, only options that are different from production/base configuration.
在此文件中,您不需要指定所有选项,只需指定与生产/基础配置不同的选项。
More info in official docs
官方文档中的更多信息
回答by Edwin M
If you are on Linux and Mac, you may determine your "hostname" using the "hostname" terminal command and use it in the array.
如果您使用的是 Linux 和 Mac,您可以使用“hostname”终端命令确定您的“主机名”并在数组中使用它。
回答by alexdmejias
I know this is kind of late but it might help somebody who ends up in this thread. To answer your question of loading custom config files, you can find more details here. I didn't see it anywhere in the docs, but all you have to do is just make a new directory in your config/
with the name of your environment. Duplicate the file that you want to overwrite to your new directory and edit the settings. While you are in that file you can also remove everything that is not being overwritten.
我知道这有点晚了,但它可能会对最终出现在此线程中的人有所帮助。要回答加载自定义配置文件的问题,您可以在此处找到更多详细信息。我在文档中的任何地方都没有看到它,但是您所要做的就是在您config/
的环境中创建一个新目录。将要覆盖的文件复制到新目录并编辑设置。当您在该文件中时,您还可以删除所有未被覆盖的内容。