Laravel 获取配置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49183829/
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
Laravel Get Config Variable
提问by giuseppe
In Laravel 5.0 (I know it is old, but the project is not mine) I set in config/app.php
在 Laravel 5.0(我知道它很旧,但该项目不是我的)中 config/app.php
return [
...
'languages' => ['en','it'],
...
]
Then, I have a blade wrapper in resources/views/frontend/includes/menus/guest.blade.php
然后,我有一个刀片包装 resources/views/frontend/includes/menus/guest.blade.php
@foreach (Config::get('languages') as $lang => $language)
But, Laravel says that foreach has no valid argument, which means that Config::get('languages') returns null. I can't set custom variables in app.php?
但是,Laravel 说 foreach 没有有效的参数,这意味着 Config::get('languages') 返回 null。我无法在 app.php 中设置自定义变量?
回答by Luke
Laravel has a helper function for config which allows you to avoid instantiating a Config
instance each time you access a value.
Laravel 有一个用于 config 的辅助函数,它允许您避免Config
每次访问一个值时实例化一个实例。
Simply use:
只需使用:
config('app.languages');
回答by Jonathan
You need to change it to:
您需要将其更改为:
@foreach (Config::get('app.languages') as $lang => $language)
.
@foreach (Config::get('app.languages') as $lang => $language)
.
Treat the first segment of your lookup as the files under /config
, in this case app.php
corresponds to Config::get('app.*')
将查找的第一段视为 下的文件/config
,在这种情况下app.php
对应于Config::get('app.*')