使用 env('APP_ENV')、config('app.env') 或 App::environment() 获取应用环境有什么区别? [英] What is difference between use env('APP_ENV'), config('app.env') or App::environment() to get app environment?

查看:17
本文介绍了使用 env('APP_ENV')、config('app.env') 或 App::environment() 获取应用环境有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 env('APP_ENV')config('app.env')App::environment() 获取应用环境?

What is difference between use env('APP_ENV'), config('app.env') or App::environment() to get app environment?

我知道env('APP_ENV')会去$_ENVconfig('app.env')读取配置App::environment() 是所有的抽象.在我看来,优势就是这个.抽象.

I know that the env('APP_ENV') will to $_ENV, config('app.env') reads the configuration and App::environment() is an abstraction of all. And in my opinion the advantage is even this. Abstraction.

我不知道是否还有其他差异,例如性能或安全级别

I do not know if there are other differences, such as the level of performance or security

推荐答案

简而言之2020 年最新:

  • 仅在配置文件中使用 env()

使用 App::environment() 来检查环境(.env 中的 APP_ENV).

use App::environment() for checking the environment (APP_ENV in .env).

对所有其他环境变量使用 config('app.var'),例如:config('app.debug')

use config('app.var') for all other env variables, ex: config('app.debug')

为您自己的 ENV 变量创建自己的配置文件.示例:
在你的 .env 中:

create own config files for your own ENV variables. Example:
In your .env:

MY_VALUE=foo

示例 config app/myconfig.php

return [
    'myvalue' => env('MY_VALUE', 'bar'), // 'bar' is default if MY_VALUE is missing in .env
];

在您的代码中访问:

config('myconfig.myvalue') // will result in 'foo'


说明&历史:

我只是觉得.当您缓存配置文件时,env() 将(有时?)无法正常工作.所以我发现了:

I just felt over it. When you cache your config file, env() will (sometimes?) not work right. So what I found out:

  1. Laravel 建议只在配置文件中使用 env().在您的代码中使用 config() 助手而不是 env().例如,您可以在代码中调用 config('app.env').
  2. 当您使用 php artisan config:cache 时,所有配置字符串都被框架缓存,并且您对 .env 文件所做的任何更改都不会生效,直到您再次运行 php artisan config:cache 命令.
  1. Laravel recommends only to use env() within the config files. Use the config() helper in your code instead of env(). For example you can call config('app.env') in your code.
  2. When you use php artisan config:cache all the configuration strings are cached by the framework and any changes you make to your .env file will not be active until you run the php artisan config:cache command again.

来自 Laracast 上的 这篇文章:

From this article on Laracast:

更新:
只要您不使用 php artisan config:cacheenv() 调用就可以工作.所以这是非常危险的,因为它经常在开发时工作但在生产时会失败.请参阅升级指南

UPDATE:
env() calls work as long as you don't use php artisan config:cache. So it's very dangerous because it will often work while development but will fail on production. See upgrade guide

缓存和环境

如果您在部署期间使用 config:cache 命令,则必须确保您只从您的内部调用 env 函数配置文件,而不是来自应用程序中的任何其他位置.

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

如果你是在你的应用程序中调用 env,它是强烈的建议您在配置中添加适当的配置值文件并从该位置调用 env ,允许您转换你的环境调用配置调用.

If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.

更新 Laravel 5.6:
Laravel 现在在其文档中推荐使用

$environment = App::environment();

// or check on an array of environments:
if (App::environment(['local', 'staging'])) {
    // The environment is either local OR staging...
}

描述 env() 只是从配置文件中的 .env 中检索值,例如 config('app.env')config('app.debug').

and describes that env() is just to retrieve values from .env in config files, like config('app.env') or config('app.debug').

这篇关于使用 env('APP_ENV')、config('app.env') 或 App::environment() 获取应用环境有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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