简体   繁体   English

访问在Laravel5的自定义类中创建的新配置文件

[英]Access new Config file created in custom classes in Laravel5

How can I access new config file(app_category.php) in Custom class?? 如何在自定义类中访问新的配置文件(app_category.php)?

Here is how I am trying to access config properties here. 这是我尝试在此处访问配置属性的方式。 I want to create some functions related to config file. 我想创建一些与配置文件相关的功能。

<?php namespace App\Classes {
    use Illuminate\Support\Facades\App;

    class Common {

        public $category = \Config::get('app_category.categories');
        public function getAllCategories()
        {
            return $this->category;
        }

        /**
         * @param mixed $category
         */
        public function setCategory($category)
        {
            $this->category = $category;
        }
    }
}
?>

You can't have an expression (in this case it's a function call) as the default value of a member variable. 您不能将表达式(在这种情况下为函数调用)作为成员变量的默认值。 Instead you should assign it in your constructor : 相反,您应该在构造函数中分配它:

class Common {

    public $category;

    public function __construct(){
        $this->category = \Config::get('app_category.categories');
    }

// etc...

I personally prefer the config() helper function over the facade. 我个人更喜欢config()辅助函数而不是外观。 I just wanted to let you know it existed in case you didn't already. 我只是想让您知道它的存在,以防您还没有。

$this->category = config('app_category.categories');

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

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