How does config:cache work in laravel?

Do you like this post? Visiting yish.dev to get the latest post 🎉

We all know if you want to accelerate performance in laravel, you can do a lot of things, one of the things is cache what you think, likely route, config, view, etc, but how does it work in laravel?

To be noticed, cache DOES NOT cache your environment variables, there is documentation in official describe.

Upgrade Guide — Laravel — The PHP Framework For Web Artisans

Caching And Env

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

When you type $ php artisan config:cache what happen to your application?

Next, we explore the method getFreshConfiguration

To see getCachedConfigPath and normalizeCachePath

But here is a problem, the laravel how to know to load your configs cached or not?

And now we return back to see getFreshConfiguration and you are seeing

$app->make(ConsoleKernelContract::class)->bootstrap();

Next, seeing Illuminate\Foundation\Bootstrap\LoadConfiguration::class

For now, we all knew how does it work about config cache, at last, I found a tip about load bootstrap loading.

If you want to load the environment after bootstrapping, you can use afterLoadingEnvironment closure.

Cheers!

The Explorer 🍻 yish.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store