7

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.

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. But what I didn't know is only that laravel load all configs at boot time. And I know that it is not real cache, I just used it as 'variables cache'. So if laravel load all configs at the start, I can call config('app.key') every time I need this config and not to worry that it slows down the application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.