默认情况下,Laravel 使用您的主机名,正如您所说 - 但是您也可以将闭包传递给该detectEnvironment
方法以使用更复杂的逻辑来设置您的环境。
像这样的东西,例如:
$env = $app->detectEnvironment(function()
{
// if statements because staging and live used the same domain,
// and this app used wildcard subdomains. you could compress this
// to a switch if your logic is simpler.
if (isset($_SERVER['HTTP_HOST']))
{
if (ends_with($_SERVER['HTTP_HOST'], 'local.dev'))
{
return 'local';
}
if (ends_with($_SERVER['HTTP_HOST'], 'staging.server.com'))
{
return 'staging';
}
if (ends_with($_SERVER['HTTP_HOST'], 'server.com'))
{
return 'production';
}
// Make sure there is always an environment set.
throw new RuntimeException('Could not determine the execution environment.');
}
});
然而,这不涉及工匠 -HTTP_HOST
不会在那里设置。如果不同的站点在不同的用户下运行,您可以使用$_SERVER['USER']
例如执行另一个单独的 switch 语句。如果不是,您还可以使用安装路径作为区分方式。