1

I'm working on a project where for each user there will be separate subdomain for example my website name is xyz.com then for user it will be user.xyz.com, for that im trying to do subdomain routing but its not working.

Below are the routes for main domain

 Route::get('/', function () {
    return redirect('login');
});

Auth::routes();

Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

Below are routes for subdomain

 Route::domain('user.xyz.com')->group(function () {
    Route::get('/posts', function () {
        return 'Second subdomain landing page';
    });
   
});

Any expert please look into this. Suggestions are welcome.

Priyanka
  • 83
  • 6

2 Answers2

1

I have a suggestion... why not pass the domain as a parameter the way you would an unknown, article id for example /article/{id}?

Try this:

Route::domain('{user}.xyz.com')->group(function () {
    Route::get('/posts', [PostsController::class, 'view'])->name('posts');
});

In our PostsController output the results...

class PostsController {
    public function view ($user){
        dd($user) //this will output the current user's subdomain name
    }
}

Let me know if it works out for you.

Relcode
  • 505
  • 1
  • 6
  • 16
  • This should work for simple vanity urls if a wildcard is set up on the DNS. However, if this is a multi-tenant application with each user having separate space with their own routing, assets, controllers..etc this really won't work. You're going to need not only separate namespaces but routing files and a middleware to do all the dynamic database stuff...etc –  May 02 '22 at 20:14
0

Try to add a domain line in RouteServiceProvider.

Route::middleware("web")->domain('Domain Name')->namespace($this->namespace)->group(base_path("routes/web.php"));
Zac Grierson
  • 656
  • 1
  • 6
  • 21