我应该在配置文件之外使用env()吗? [英] Should I be using env() outside my config files?

查看:76
本文介绍了我应该在配置文件之外使用env()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了这个 https://laravel.com/docs/5.4/configuration文档中的#configuration-caching ,这让我有些困惑.

I stumbled across this https://laravel.com/docs/5.4/configuration#configuration-caching in the documentation and it confused me a bit.

当我想要一个环境变量时,我使用env()函数返回我想要的.根据上面的链接,它说我应该改用config()函数,以确保在生产时我通过缓存访问这些值.

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')

env('APP_URL')

vs

config('app.url')

config('app.url')

那么我应该在我的应用程序中使用config()env()吗?

So should I be using config() or env() inside my app?

我假设如果添加新的env变量,我还需要更新配置文件吗?

I assume that if I add a new env variable I will also need to update my config files?

推荐答案

绝对不要在代码中直接使用env().使用config()是一个好习惯.在config文件中,使用env().env文件中获取数据.

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.

要提高应用程序的速度,应使用config:cache Artisan命令将所有配置文件缓存到单个文件中.

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:

通常应在生产部署例程中运行php artisan config:cache命令.如果在部署过程中执行config:cache命令,则应确保仅从配置文件中调用env函数.

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.

这篇关于我应该在配置文件之外使用env()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆