我可以在 Laravel 的路由组中对多个域进行分组吗?

2022-01-02 00:00:00 routing php laravel laravel-routing

假设我有以下内容:

Route::group(array('domain' => array('admin.example.com')), function()
{
    ...
});

Route::group(array('domain' => array('app.example.com')), function()
{
    ...
});

Route::group(array('domain' => array('dev.app.example.com')), function()
{
    ...
});

有没有办法让多个域共享一个路由组?类似的东西:

Is there any way to have multiple domains share a routing group? Something like:

Route::group(array('domain' => array('dev.app.example.com','app.example.com')), function()
{
    ...
});

推荐答案

Laravel 似乎不支持这个.

Laravel does not seem to support this.

我不知道为什么我没有早点想到这一点,但我想一种解决方案是在单独的函数中声明路由,并将其传递给两个路由组.

I'm not sure why I didn't think of this sooner, but I guess one solution would be to just declare the routes in a separate function as pass it to both route groups.

Route::group(array('domain' => 'admin.example.com'), function()
{
    ...
});

$appRoutes = function() {
    Route::get('/',function(){
        ...
    }); 
};

Route::group(array('domain' => 'app.example.com'), $appRoutes);
Route::group(array('domain' => 'dev.app.example.com'), $appRoutes);

我不确定此解决方案是否有任何显着的性能影响.

I'm not sure if there is any significant performance impact to this solution.

相关文章