Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does laravel cache the config?

Say, I get config with config('app.configKey') the first time. Laravel load file app and get the needed key. If I call this the next time, does laravel load the file again? Or it stores the value?

I want to know if I should write:

$value = config('app.key');
/* Some code here ... */
$anotherVar = $value;

or:

$value = config('app.key');
/* Some code here ... */
$anotherVar = config('app.key');

It is just an example. In real code I get the config in one class. And later I get this config again in another class.

like image 454
FreeLightman Avatar asked Nov 27 '15 17:11

FreeLightman


1 Answers

At boot time, Laravel reads all your configuration files and holds their values in the Application instance. So calling config('app.configKey') will not load the the config/app.php file, even when calling it the first time.

In your scenario, it really depends what the code behind /* Some code here ... */ does.

Imagine calling some method, e.g. $this->changeConfigValue(), that changes the config value for app.configKey. What that actually does is it changes the value in the Application instance, it will not overwrite your config files.

In your first code example, $anotherVar would always be equal to $value. But in the second code example, the value for $anotherVar would be read from the App instance again and would be equal to whatever $this->changeConfigValue() set the value to.

This is not a cache.

Laravel provides a way to cache your config. You can do this manually with an artisan command:

php artisan config:cache

This will create the file bootstrap/cache/config.php which holds all your configuration in one single file. When booting, it is faster to read just one file instead of your entire config directory (and that's what a cache is for :P).

http://laravel.com/docs/5.1/installation#configuration-caching

like image 61
tommy Avatar answered Oct 10 '22 23:10

tommy