在 Laravel 5 中添加新的配置文件不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28794861/
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
Adding a new config file in Laravel 5 not working
提问by geoffs3310
I want a new config file in my Laravel 5 app to store all my constants in. After looking around the net I found the recommended solution seems to be to create a new config file that returns an array of key value pairs and then use that. So I created the following file:
我想在我的 Laravel 5 应用程序中创建一个新的配置文件来存储我的所有常量。在环顾网络后,我发现推荐的解决方案似乎是创建一个新的配置文件,该文件返回一组键值对,然后使用它。所以我创建了以下文件:
<?php
// config/constants.php
return [
'SITE_NAME' => 'Site Name',
'SITE_EMAIL' => '[email protected]',
'ADMIN_EMAIL' => '[email protected]'
];
Then in one of my controllers I try to access one of these values like so:
然后在我的一个控制器中,我尝试访问这些值之一,如下所示:
echo Config::get('constants.ADMIN_EMAIL');
I just get the following error:
我只是收到以下错误:
FatalErrorException in WelcomeController.php line 46:
Class 'App\Http\Controllers\Config' not found
Do I have to do something else to get it to work?
我是否必须做其他事情才能使其正常工作?
回答by Bernig
In Laravel 5, to avoid this kind of headache, you can use the confighelper function to get a config item, like this :
在 Laravel 5 中,为了避免这种头痛,您可以使用confighelper 函数来获取配置项,如下所示:
config('constants.ADMIN_EMAIL')
config('constants.ADMIN_EMAIL')
Nice and easy ;)
好,易于 ;)
回答by lukasgeiter
The Configclass is an alias in the global namespace. To reference it from inside the controller (which is in the App\Http\Controllersnamespace) you have to prepend it with a backslash:
该Config班是全局命名空间的别名。要从控制器内部(在App\Http\Controllers命名空间中)引用它,您必须在它前面加上一个反斜杠:
echo \Config::get('constants.ADMIN_EMAIL');
Or add a usestatement above the controller class:
或者use在控制器类上面添加一条语句:
use Config;
class MyController extends Controller {
As an alternative you might also want to use dependency injectionto access the config. That would look somewhat like this:
作为替代方案,您可能还想使用依赖注入来访问配置。这看起来有点像这样:
class MyController extends Controller {
public function __construct(Illuminate\Config\Repository $config){
$this->config = $config;
}
public function index(){
echo $this->config->get('constants.ADMIN_EMAIL');
}
}
As @Bernig suggests you can also simply use the new config()helper function:
正如@Bernig 建议的那样,您也可以简单地使用新的config()辅助函数:
echo config('constants.ADMIN_EMAIL');
回答by Jinsong Li
I met the same issue today, and I find an elegant solution:
add the config/your_new_config.phpto ConfigServiceProvider, like this:
我今天遇到了同样的问题,我找到了一个优雅的解决方案:添加config/your_new_config.phpto ConfigServiceProvider,如下所示:
/**
* Overwrite any vendor / package configuration.
*
* This service provider is intended to provide a convenient location for you
* to overwrite any "vendor" or package configuration that you may want to
* modify before the application handles the incoming request / command.
*
* @return void
*/
public function register()
{
config([
'config/your_new_config.php', // add your new config file here!
]);
}
The reason is well explained in the function's comments
原因在函数的注释中有很好的解释

