Php Laravel DetectenEnvironment-如何组合机器名和服务器变量?
在Laravel4.2中,我使用App::detectEnvironment根据机器名更改数据库。我现在还需要让它根据环境变量改变环境。我尝试过组合,但目前还不起作用,我不知道如何组合这两种技术 使用计算机名:Php Laravel DetectenEnvironment-如何组合机器名和服务器变量?,php,laravel,laravel-4,environment-variables,Php,Laravel,Laravel 4,Environment Variables,在Laravel4.2中,我使用App::detectEnvironment根据机器名更改数据库。我现在还需要让它根据环境变量改变环境。我尝试过组合,但目前还不起作用,我不知道如何组合这两种技术 使用计算机名: $env = $app->detectEnvironment(array( 'local' => array('.local', 'homestead'), 'staging' => array('ip-staging
$env = $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
)
);
使用服务器环境变量:
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
}
非工作组合代码如下所示:
$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')
);
});
这应该可以做到。如果设置了,它将环境设置为您在
getenv('LARAVEL_ENV')
中拥有的环境,否则它将使用默认的$app->detectEnvironment
方法
$env = getenv('LARAVEL_ENV') ?: $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
找到了答案-多亏@Marwelln的提示
$env
变量需要来自detectEnvironment
函数,才能被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')
));
}
您也尝试过使用超级全局变量吗<代码>返回isset($_ENV['LARAVEL_ENV'])$_ENV['LARAVEL_ENV']:数组(…)代码>-可替换为$\u服务器