使用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')
, 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')
将 $ _ ENV
, config('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 peformance or security
推荐答案
我只是感受到了。当您缓存配置文件时,env()将(有时?)无法正常工作。所以我发现:
I just felt over it. When you cache your config file, env() will (sometimes?) not work right. So what I found out:
- Laravel建议只能在配置文件中使用env()。在代码中使用config()帮助器,而不是env()。例如,您可以在代码中调用config('APP_ENV')。
- 当您使用php artisan config时:缓存所有的配置字符串都被框架缓存,并且您对.env文件将不会激活,直到您再次运行php artisan config:cache命令。
从这里:
< a href =https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes =nofollow noreferrer> https://laracasts.com/discuss/channels/general有时候 - 讨论/ env-not-reading-variables-
更新:
env )调用工作只要你不使用php artisan config:cache。所以这是非常危险的,因为它通常会在开发过程中工作,但会在生产中失败。
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.
从这里: https://laravel.com/docs/5.2/upgrade#upgrade-5.2.0
这篇关于使用env('APP_ENV'),config('app.env')或App :: environment()来获取应用程序环境有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!