php - 子域路由覆盖并运行 web.php 中的最后一个子域

标签 php mysql azure laravel-5

我需要根据点击 URL 的子域来更改数据库配置。但现在,所有子域都将被最后一个子域覆盖。

我创建了一组带有子域的路由,并使用 Config::set 函数配置了数据库。这可行,但始终采用最后一个子域组。

Route::domain('s1.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
                'driver' => 'mysql',
        'host' => 'localhost',
        'port' => env('DB_PORT', 'xxxx'),
        'database' => 'xx',
        'username' => 'xxx',
        'password' => 'xxxx',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null
]);
Route::resource('/', 'LoginController');
});
Route::domain('s2.xyz.com')->group(function () {
\Config::set("database.connections.mysql", [
                'driver' => 'mysql',
        'host' => 'localhost',
        'port' => env('DB_PORT', 'yyyy'),
        'database' => 'yy',
        'username' => 'yyy',
        'password' => 'yyyy',
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null
]);
Route::resource('/', 'LoginController');
});

当给出 s1.xyz.com 时,应使用 xx 数据库,并且 URL 只能是 s1.xyz.com。 但现在虽然我提供了 s1.xyz.com,但一旦我点击登录表单中的提交按钮,它就会重定向到 s2.xyz.com

最佳答案

$domain = substr (Request::root(), 7);
switch($domain){
case 's1.xy.com':    
Route::domain('s1.xyz.com')->group(function () {
    \Config::set("database.connections.mysql", [
                    'driver' => 'mysql',
            'host' => 'localhost',
            'port' => env('DB_PORT', 'xxxx'),
            'database' => 'xx',
            'username' => 'xxx',
            'password' => 'xxxx',
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null
    ]);
    Route::resource('/', 'LoginController');
    });
break;
case 's2.xyz.com'
    Route::domain('s2.xyz.com')->group(function () {
    \Config::set("database.connections.mysql", [
                    'driver' => 'mysql',
            'host' => 'localhost',
            'port' => env('DB_PORT', 'yyyy'),
            'database' => 'yy',
            'username' => 'yyy',
            'password' => 'yyyy',
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null
    ]);
    Route::resource('/', 'LoginController');
    });
break;}

关于php - 子域路由覆盖并运行 web.php 中的最后一个子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57453663/

相关文章:

azure - 用于获取 Azure Monitor 日志的 AZ 命令

php - 在 Migrator.php 行 418 : Class 'Table' not found error on running a migration

mysql恢复外键错误

Mysql选择组合

php - 显示 foreach 循环的总和

azure - 在 Azure Pipeline 中安装来自 Azure DevOps Artifact Feed 的包

azure - 如何查找 Azure 的 DOMAIN.onmicrosoft.com 域?

php - 如何在 PHP 中创建随机唯一的字母数字字符串

php - array_multisort 具有多维对象数组

php - 为什么 smarty greater than function 不起作用?