Laravel 5 配置文件中的自定义配置文件访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30152032/
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
Custom config file access within Laravel 5 config files
提问by korokoro
I created custom config file in Laravel 5and try to use settings from it in other files (session.php
, cache.php
) by calling config('myconfigfile.value')
, but there are no values returned from my config. It seems that config files have predefined loading order and custom configs is loading in the end or it didn't initialized else by other cause.
我在Laravel 5 中创建了自定义配置文件,并尝试通过调用在其他文件 ( session.php
, cache.php
) 中使用它的设置config('myconfigfile.value')
,但我的配置没有返回任何值。似乎配置文件具有预定义的加载顺序,并且自定义配置最终正在加载,或者它没有因其他原因进行初始化。
How can I access to my settings from laravel config files?
如何从 Laravel 配置文件访问我的设置?
回答by Ali Mohammed
First you need add the file in the config folder for example: laravel\config\Test.php
首先你需要在 config 文件夹中添加文件,例如:laravel\config\Test.php
<?php
use Illuminate\Support\Facades\Config;
return [
'name' => 'Ali Mohammed',
'age' => 26
];
then you need to call the config
然后你需要调用配置
get('test', function(){
return Config::get('Test.name');
});
回答by Quetzy Garcia
Why not just use the env()
helper within every configuration file you need?
为什么不在env()
您需要的每个配置文件中使用帮助程序?
You would just have to set the value in your .env
file
您只需要在.env
文件中设置值
CUSTOM_SETTING="some value"
and get it on each configuration file
并在每个配置文件上获取它
<?php
return [
// ...
'custom_value' => env('CUSTOM_SETTING', 'some default value'),
// ...
];
Have a look at the helper code.
查看帮助程序代码。