skip to Main Content

I am building an application where users are registered and want to be redirected to their individual dashboard like this .

http://localhost/project/{username}/dashboard,

Now, its happening like

localhost/project/vendors/dashboard (here all users are accessing same URL)

but I want to make it like :

http://localhost/project/{username1}/dashboard, http://localhost/project/{username2}/dashboard

Googled lot but none of them are explained well and working.

Please assist with complete flow.

  1. I want to declare the value of {username} globally and use it in route as prefix.
  2. I dont want to use it before each name route. will use it as prefix and group with all vendors routes

I have made this, and its working as

localhost/project/vendors/dashboard

Route::prefix('vendors')->group(function () { Route::middleware(['auth:vendor'])->group(function () { Route::get('/dashboard', [VendorController::class, 'dashboard'])->name('vendor.dashboard'); });
});

2

Answers


  1. You can specify route parameters in brackets like so {parameter}, change your code into this.

    Route::get('project/{username}/dashboard', [UserDashboardController::class, 'dashboard'])
        ->name('user.dashboard');
    

    In your controller you could access it like this.

    class UserDashboardController
    {
        public function dashboard(string $username)
        {
            User::where('username', $username)->firstOrFail();
    
            // something else
        }
    }
    

    Seems like in your routes your are mixing vendor prefix logic in with something that in your specifications of what your urls should look like does not match. Which i think is making up some of the confusion on this case.

    Login or Signup to reply.
  2. You can use route prefix like this

    Route::prefix('{username}')->group(function () {
        Route::middleware(['auth'])->group(function () {
            Route::get('/dashboard', [UserController::class, 'dashboard'])->name('user.dashboard');
        });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search