0

我在 Laravel 中编写了一个后端,需要在同一物理服务器上部署两次。我需要为这些使用两个不同的数据库,但由于它们位于同一台服务器上,我无法使用 Laravel 中的内置主机检测。

目前,我已经通过将我的配置文件包装在这段代码中来“修复”这个问题:

if ($_SERVER["HTTP_HOST"] === "example.com") {
    return config array...
} else if ($_SERVER["HTTP_HOST"] === "example.net") {
    return config array...
}

但这打破了工匠,所以没有更多php artisan down|upor php artisan cache:clear

必须有更好的方法来实现这一点,不是吗?

4

1 回答 1

2

默认情况下,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 语句。如果不是,您还可以使用安装路径作为区分方式。

于 2015-03-04T12:39:41.057 回答