gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:42:32 24 4
gpt4 key购买 nike

我需要根据点击 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com