简体   繁体   English

在Laravel 5中添加新的配置文件无法正常工作

[英]Adding a new config file in Laravel 5 not working

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. 我希望在我的Laravel 5应用程序中有一个新的配置文件来存储我的所有常量。在浏览网络后,我发现建议的解决方案似乎是创建一个新的配置文件,返回一组键值对然后使用它。 So I created the following file: 所以我创建了以下文件:

<?php
// config/constants.php

return [
    'SITE_NAME' => 'Site Name',
    'SITE_EMAIL' => 'email@site.com',
    'ADMIN_EMAIL' => 'admin@site.com'
];

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? 我是否必须做其他事情才能让它发挥作用?

In Laravel 5, to avoid this kind of headache, you can use the config helper function to get a config item, like this : 在Laravel 5中,为了避免这种麻烦,您可以使用config helper函数来获取配置项,如下所示:

config('constants.ADMIN_EMAIL')

Nice and easy ;) 好,易于 ;)

The Config class is an alias in the global namespace. Config类是全局命名空间中的别名。 To reference it from inside the controller (which is in the App\\Http\\Controllers namespace) you have to prepend it with a backslash: 要从控制器内部(在App\\Http\\Controllers命名空间中)引用它,您必须在前面加上反斜杠:

echo \Config::get('constants.ADMIN_EMAIL');

Or add a use statement above the controller class: 或者在控制器类上面添加一个use语句:

use Config;

class MyController extends Controller {

As an alternative you might also want to use dependency injection to 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');

I met the same issue today, and I find an elegant solution: add the config/your_new_config.php to ConfigServiceProvider , like this: 我今天遇到了同样的问题,我找到了一个优雅的解决方案:将config/your_new_config.php添加到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 原因在函数的评论中得到了很好的解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM