I stumbled across this https://laravel.com/docs/5.4/configuration#configuration-caching in the documentation and it confused me a bit.
When I want an environment variable I use the env() function to return what I want. According to the above link it says I should be using the config() function instead to ensure that on production I am accessing the values through a cache.
e.g. These both return the same thing
env('APP_URL')
vs
config('app.url')
So should I be using config()
or env()
inside my app?
I assume that if I add a new env variable I will also need to update my config files?
You should never use
env()
in the code directly. It's a good practice to useconfig()
. Inconfig
files useenv()
to get the data from.env
file.In this case, you can easily override config values at runtime or during testing.
You also can use config caching.
Another reason is described in the docs: