Cache facade not working in Laravel 5

2019-09-10 09:38发布

问题:

I want some settings to be user-manageable. Because of this, I am storing them in database, retrieving and caching them afterwards. However, when I try to call Cache facade inside the config/config.php file, I get an error. Details are given below.

I have following code located in app/config/custom.php:

<?php 
// app/config/custom.php

use Illuminate\Support\Facades\Cache;

return [
   'foo' => Cache::get('foo');
];

Which spits out a following error:

Fatal error: Call to a member function get() on a non-object in D:\www\project\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 216

How to fix this?

回答1:

As discussed in the comments, config files are not meant to store dynamic application values, if it's dynamic (i.e. User specific values), you should handle them in different parts of the application.

You can make something available globally throughout your application by using Service Providers.