Laravel, how to get the environment value?

LaravelLaravel 4

Laravel Problem Overview


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

Laravel Solutions


Solution 1 - Laravel

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

> Added App::environment method.

Edit: these are now a number of various ways to get the environment variable as of 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()

Solution 2 - Laravel

You can also use app()->env.

Solution 3 - Laravel

In Laravel 4 and 5, the Laravel official docs suggest using:

$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...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionduality_View Question on Stackoverflow
Solution 1 - LaravelLaurenceView Answer on Stackoverflow
Solution 2 - LaravelkfriendView Answer on Stackoverflow
Solution 3 - LaravelJustinView Answer on Stackoverflow