65

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

Justin
  • 26,443
  • 16
  • 111
  • 128
duality_
  • 17,738
  • 23
  • 77
  • 95

3 Answers3

105

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()

...or 'production'

App::isProduction()
app()->isProduction()

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

App::runningUnitTests()
app()->runningUnitTests()
Joel Mellon
  • 3,672
  • 1
  • 26
  • 25
Laurence
  • 58,936
  • 21
  • 171
  • 212
  • Hmmm, don't have the Beta 4 yet, do you know how I can upgrade? – duality_ Feb 18 '13 at 16:48
  • simple - run "composer update". Then read the 2nd half of this answer - which talks about how to update your app install aswell: http://stackoverflow.com/q/14909192/1317935 – Laurence Feb 18 '13 at 16:53
27

You can also use app()->env.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
kfriend
  • 2,584
  • 1
  • 19
  • 15
24

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...
}
Justin
  • 26,443
  • 16
  • 111
  • 128