Can I group multiple domains in a routing group in

2019-02-01 07:35发布

Let's say I have the following:

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()
{
    ...
});

8条回答
Summer. ? 凉城
2楼-- · 2019-02-01 08:22

You can pass on the domain name as well:

Route::pattern('domain', '(domain1.develop|domain2.develop|domain.com)');
Route::group(['domain' => '{domain}'], function() {
    Route::get('/', function($domain) {
        return 'This is the page for ' . $domain . '!';
    });
});

Just in case you need to know with which domain name the controller is called. Tested it with Laravel 5.6.

查看更多
戒情不戒烟
3楼-- · 2019-02-01 08:31

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.

查看更多
登录 后发表回答