Should I be using env() outside my config files?

后端 未结 1 332
长发绾君心
长发绾君心 2021-01-12 00:26

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 var

相关标签:
1条回答
  • 2021-01-12 01:08

    You should never use env() in the code directly. It's a good practice to use config(). In config files use env() 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.

    To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command.

    Another reason is described in the docs:

    You should typically run the php artisan config:cache command as part of your production deployment routine. 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.

    0 讨论(0)
提交回复
热议问题