Alex197
Alex197

Reputation: 923

How to create same route name for differents subdomains in Laravel?

I'm actually using nwidart/laravel-modules on Laravel 5.8 and I got problem with some routes.

I created some modules (module_1, module_2, module_3).

Each module has file web.php, and in these files, I added these lines:

module_1 web.php:

Route::domain('module1.domain.com')->group(function(){
    Route::get('/', 'ModuleOneController@index')->name('home');
});

module_2 web.php:

Route::domain('module2.domain.com')->group(function(){
    Route::get('/', 'ModuleTwoController@index')->name('home');
});

module_3 web.php:

Route::domain('module3.domain.com')->group(function(){
    Route::get('/', 'ModuleThreeController@index')->name('home');
});

And in my main web.php in laravel project:

Route::domain('domain.com')->group(function(){
    Route::get('/', 'HomeController@index')->name('home');
});

I can access to these urls and each controllers are called on each subdomains, but when I want to call the function route('home'), it always returns https://module3.domain.com event if I specified each route names in different domain.

I would to get:

route('home') => 'https://module1.domain.com' when I call it in module1.domain.com

route('home') => 'https://module2.domain.com' when I call it in module2.domain.com etc...

Is there a way to call the same route name in different domain and get differents results ?

Thank in advance

Upvotes: 1

Views: 587

Answers (2)

Nick
Nick

Reputation: 546

Route::group(['domain' => 'module.domain.com'], function (){
    Route::post('login', [
        'as' => 'login',
        'uses' => 'AuthController@login'
    ]);
    // ... all route
});

Route::group(['domain' => 'module1.domain.com'], function (){        
    // ... all route
});

Upvotes: 2

Abhay Maurya
Abhay Maurya

Reputation: 12277

I have never used the package "nwidart/laravel-modules". However, typically laravel uses the APP_URL variable available in your .env file to actually match the domain in routes. Maybe it's defined as "https://module3.domain.com" in your .env file and that is why its matching that route only.

Maybe there is a way to define multiple APP_URLs using the "nwidart/laravel-modules" package?

This is not a solution but it might lead you in the right direction. Otherwise, someone will have to study the package details in order to actually understand the issue.

Upvotes: 1

Related Questions