laravel 主域路由优先于动态子域

laravel main domain route is taking precedence over dynamic subdomain

我正在做一个 Laravel 项目,并且已经构建了一个创建动态子域的功能,这非常好。这是路由代码。

Route::group(['domain' => '{subdomain}.{domain}.{ext}'], function($subdomain)
{ 
    Route::get('/', 'UserController@userPage')->name('userPage');
    Route::post('/', 'UserController@userPageSave')->name('userPageSave');

});

子域工作正常,但一旦我尝试访问主域,它就优先于子域

Route::get('/', function() {
    return view('general.homepage');
});

所以我输入了这个主路由代码,然后我就不能再访问子域了。子域现在基本上显示主域中的任何内容。这令人沮丧。

来自laravel 文档 https://laravel.com/docs/5.8/routing#route-group-sub-domain-routing

In order to ensure your sub-domain routes are reachable, you should register sub-domain routes before registering root domain routes. This will prevent root domain routes from overwriting sub-domain routes which have the same URI path.

来自 laravel 8。 https://laravel.com/docs/7.x/routing#route-group-subdomain-routing

In order to ensure your subdomain routes are reachable, you should register subdomain routes before registering root domain routes. This will prevent root domain routes from overwriting subdomain routes which have the same URI path.

如果你不明白上面引用的意思,因为这基本上是这种情况下的问题,它简单地意味着你将你的子域路由放置(注册)在任何根域路由的正上方。

你应该做什么:

//Rooot Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});

//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

你应该 :


//Sub Domain route(s)

Route::domain('admin.site')->group(function () {
      Route::get('/', function () {
        return "I will only trigger when domain is admin.site.";
    });
});

//Root Domain route(s)
    Route::get('/', function () {
    return view('welcome'); 
});