Home > Enterprise >  Laravel 8 working with subdomain routing but its not working, I'm little confused about this
Laravel 8 working with subdomain routing but its not working, I'm little confused about this

Time:05-03

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.

CodePudding user response:

Try to add domain line in RouteServiceProvider.

Route::middleware("web")->domain('Domain Name')->namespace($this->namespace)->group(base_path("routes/web.php"));

CodePudding user response:

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.

  • Related