2 months ago

Setting multiple domains for a route group

Posted 2 months ago by G89Angel


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?

Please sign in or create an account to participate in this conversation.