Laravel 4:我如何获得环境值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14935846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:27  来源:igfitidea点击:

Laravel 4: how can I get the environment value?

laravellaravel-4

提问by duality_

I know that I can access the environment value using the global $envvariable, but is there the right wayto get this value?

我知道我可以使用全局$env变量访问环境值,但是有没有正确的方法来获取这个值?

回答by Laurence

You're in luck - this was just added in Beta 4 - see here for details

你很幸运 -这只是在 Beta 4 中添加的 - 有关详细信息,请参见此处

Added App::environment method.

添加了 App::environment 方法。

Edit:these are now a number of various ways to get the environment variable as of Laravel 4.1

编辑:从 Laravel 4.1 开始,这些是获取环境变量的多种方法

App::environment()
app()->environment()
app()->env
$GLOBALS['env'] // not recommended - but it is possible

You can also specifically check for if the current environment is set to 'local'

您还可以专门检查当前环境是否设置为“本地”

App::isLocal()
app()->isLocal()

You can also specifically check for if the current environment is set to 'testing'

您还可以专门检查当前环境是否设置为“测试”

App::runningUnitTests()
app()->runningUnitTests()

回答by kfriend

You can also use app()->env.

您也可以使用app()->env.

回答by Justin

In Laravel 4 and 5, the Laravel official docssuggest using:

在 Laravel 4 和 5 中,Laravel 官方文档建议使用:

$environment = App::environment();

You may also pass arguments to the environment method to check if the environment matches a given value:

您还可以将参数传递给 environment 方法以检查环境是否与给定值匹配:

if (App::environment('local'))
{
    // The environment is local
}

if (App::environment('local', 'staging'))
{
    // The environment is either local OR staging...
}