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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:29:31  来源:igfitidea点击:

Laravel Get Config Variable

laravel

提问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 Configinstance 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.phpcorresponds to Config::get('app.*')

将查找的第一段视为 下的文件/config,在这种情况下app.php对应于Config::get('app.*')