0

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?

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87

1 Answers1

2

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.

Mina Abadir
  • 2,951
  • 2
  • 15
  • 20
  • Thanks for an answer. I think, it answers why the code above isn't working. I've asked a different question taking into account impossibility to use Facades in the config files. Please see more detailed question here: http://stackoverflow.com/questions/38673236/is-it-a-good-idea-to-edit-env-dynamically-in-laravel-5 – Alex Lomia Jul 30 '16 at 11:19
  • As discussed, my answer here is not meant to get more reputation, it's for the community benefit. That being said, it answers your question and why you are getting the error and some recommendation on how make some value available to the different application parts. The best way to store admin panel settings is through DB, and you load those values from a Mailer model in the case you have mentioned for setting mail server settings. – Mina Abadir Jul 30 '16 at 11:26
  • I would rather avoid loading settings somewhere other than config files, because it will introduce unnecessary confusion in the codebase – Alex Lomia Jul 30 '16 at 11:33
  • User specific settings are not meant to be loaded in the configuration files. Configuration files are for system wide settings. Loading user specific settings in configuration files will create the confusion you are worrying about, as it's against the standard system architecture. – Mina Abadir Jul 30 '16 at 11:56
  • I am required to implement user-changeable settings. Now, imagine, that another developer inherits the codebase. He goes to smtp settings and changes them only to find, that the changes he made have no effect! Imagine his rage and frustration, after many liters of sweat and tears, he eventually finds, that the mailer takes settings from some non-conventional place instead of the conventional one. – Alex Lomia Jul 30 '16 at 12:37
  • Indeed, we have to use [service providers](https://medium.com/@razamoh/create-own-custom-helper-functions-classes-in-laravel-e8d2f50ff38). In that case, I wanted to implement custom helpers and it didn't work to call the helpers function from the config files, but as a service provider – Pathros Mar 11 '22 at 23:12