Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

G89Angel's avatar

Setting multiple domains for a route group

Hello,

I need to add a new subdomain to my application and therefore I need to expose all my routes to that new subdomain.

My first though was to repeat the whole process of adding the routes on the RouteServiceProvider, basically creating a duplication. This works well until you try to cache the routes, where since Laravel 7 you get warned about a duplicate route name.

The second attempt seemed more elegant, using a pattern and a route group like so:

Route::pattern('domain', '(web.com|x.web.com)');
Route::group(['domain' => '{domain}'], function () {
    Route::get('/', function($domain) {
        // Now all routes receive $domain as the first parameter
    });
});

The problem now is all my routes aren't working as before since the first parameter received is the $domain, which is not what I expected. Is there a way to make Route::group keep passing the Request as the first parameter?

The last thing I though was to remove the domain part completely from my route definition and add a new middleware where I could check independently if the domain matches my expectations... but I don' quite like that...

Any suggestion?

0 likes
3 replies
aurawindsurfing's avatar

Hey @g89angel

The problem now is all my routes aren't working as before since the first parameter received is the $domain, which is not what I expected.

What did you expect to get? This is expected behaviour where $domain is being preppend with the route group.

G89Angel's avatar

I know that's the way it's programmed, but it's kinda cumbersome to handle a new route requirement like in my case, that's why I was looking for assessment of how to proceed better

Please or to participate in this conversation.