我在文档中偶然发现了这个https://laravel.com/docs/5.4/configuration#configuration-caching,这让我有些困惑.
当我想要一个环境变量时,我使用env()函数返回我想要的.根据上面的链接,它说我应该改用config()函数,以确保在生产中我通过缓存访问这些值.
例如这些都返回同一件事
ENV( ‘APP_URL’)
VS
配置( ‘app.url’)
那我应该在我的应用程序内使用config()或env()吗?
解决方法:
您永远不要直接在代码中使用env().使用config()是一个好习惯.在配置文件中,使用env()从.env文件获取数据.
在这种情况下,您可以在运行时或测试期间轻松覆盖配置值.
您还可以使用配置缓存.
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.
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 theenv
function from within your configuration files.