Reputation: 3665
In Laravel 4.2, I'm using App::detectEnvironment to change database according to machine name. I also now need to have it change environment according to an environment variable. I've tried to combine but it doesn't work at the moment and I'm not sure how to combine the two techniques.
Using machine name:
$env = $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
)
);
Using a server environment variable:
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
}
Non-working combined code looks like:
$env = $app->detectEnvironment(function()
{
// Default to machine name if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
);
});
Upvotes: 1
Views: 2708
Reputation: 3665
Found the answer - thanks to @Marwelln's hint.
The $env
variable needs to come from the detectEnvironment
function for it to be recognised by Laravel.
if (getenv('LARAVEL_ENV'))
{
$env = $app->detectEnvironment(function()
{
return getenv('LARAVEL_ENV');
});
}
else
{
$env = $app->detectEnvironment(array(
// local development environments are set with machine host names
// developers find this out by typing 'hostname' at command line
'local' => array('*.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
}
Upvotes: 2
Reputation: 29413
This should do it. It sets to environment to what you have in getenv('LARAVEL_ENV')
if it's set, otherwise it uses the default $app->detectEnvironment
method.
$env = getenv('LARAVEL_ENV') ?: $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
Upvotes: 1