Detecting the Environment with a Closure

Problem

Determining your environment is complicated.

You need more flexibility than simply checking hostnames to determine the environment for your Laravel application.

Solution

Use a Closure to determine your environment.

Edit bootstrap/start.php and use a Closure to do the detecting.

$env = $app->detectEnvironment(function()
{
    return getenv('_MY_ENVIRONMENT');
});

Discussion

There's multiple ways to detect your environment.

The $app->detectEnvironment() method can take either an array (as illustrated in the Environment Specific Configurations recipe) or a Closure.

When an array of 'env' => array('hostnames') is provided, if no match is found then the environment defaults to "production".

But if a Closure is provided then it must return something. There is no default.

comments powered by Disqus