简体   繁体   中英

Access new Config file created in custom classes in Laravel5

How can I access new config file(app_category.php) in Custom class??

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. I just wanted to let you know it existed in case you didn't already.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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