Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it "strongly recommended" not to use the env() helper when caching config files?

Tags:

config

laravel

Laravel upgrade documentation from 5.2 to 5.3 specifies the following:

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.

The question the documentation doesn't answer is: why?

Shouldn't the env() helper, when used within the application, still work properly? Can't I still use env() after the config is cached, instead of being forced to finding it in my whole project and replacing it with the config() helper, as (indirectly) suggested by the docs?

For example, if I have this in my app.php:

'env' => env('APP_ENV', 'production')

and it will get cached with config:cache, but I still use the env() helper somewhere in the code, then is there any reason at all that env('APP_ENV') would give me something else than the config('app.env')?

And how about the App::environment() call - is using it also not recommended after config:cache is used on production?

like image 848
lesssugar Avatar asked Jun 19 '17 10:06

lesssugar


People also ask

For what do the .env is used laravel?

Laravel's default .env file contains some common configuration values that may differ based on whether your application is running locally or on a production web server. These values are then retrieved from various Laravel configuration files within the config directory using Laravel's env function.

Why is env null?

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the . env file will not be loaded and all calls to the env function will return null.


1 Answers

This is because how caching works in Laravel, you have an storage/cache folder where is stored a "compiled" version of your configuration files when you use the command php artisan config:cache, so it is lighter and faster. In that caching process, Laravel reads the .env file and replaces all values using the env() helper function.

Now, to access the compiled version of the config, which is normally already in memory, you need to use the config() helper, otherwise you would be accessing a file, a much slower approach.

The reason behind disabling the env() function once you cached your configurations is to ensure the performance in the production enviroment, because it is assumed it is the situation where the compiling would take place.

In conclusion, it's the creator's decision, you may like it or not, IMO it makes things a bit cleaner and forces you to have a more optimized and organized application.

Hope this helps you.

like image 114
Asur Avatar answered Nov 12 '22 00:11

Asur