我应该在配置文件之外使用env()吗?

我在文档中偶然发现了这个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.

一个原因是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.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...