Laravel 4:我怎样才能获得环境价值?

时间:2022-05-03 15:32:36

I know that I can access the environment value using the global $env variable, but is there the right way to get this value?

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

3 个解决方案

#1


69  

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

你很幸运——这只是在Beta 4中添加的——详情请看这里。

Added App::environment method.

添加应用程序::环境的方法。

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'

如果当前环境设置为“local”,您还可以进行具体检查

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

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

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

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

#2


25  

You can also use app()->env.

你也可以使用app()->env。

#3


12  

In Laravel 4 and 5, the Laravel official docs suggest 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:

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

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

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

#1


69  

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

你很幸运——这只是在Beta 4中添加的——详情请看这里。

Added App::environment method.

添加应用程序::环境的方法。

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'

如果当前环境设置为“local”,您还可以进行具体检查

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

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

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

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

#2


25  

You can also use app()->env.

你也可以使用app()->env。

#3


12  

In Laravel 4 and 5, the Laravel official docs suggest 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:

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

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

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