Home > OS >  Same route defined with different middleware called last wirted route issue
Same route defined with different middleware called last wirted route issue

Time:08-05

Define the four routes with different middleware role wise.

Issue arrive is call DepartmentStaff Route every time.if i login Admin or Customer.

Call last written route every time.

//admin Route
Route::get('edit_profile', [UserController::class, 'edit_profile'])->name('edit_profile')->middleware(CheckAdminRole::class);
Route::post('profile_update', [UserController::class, 'profile_update'])->name('profile_update')->middleware(CheckAdminRole::class);
Route::get('change_password', [UserController::class, 'change_password'])->name('change_password')->middleware(CheckAdminRole::class);
Route::post('password_update', [UserController::class, 'password_update'])->name('password_update')->middleware(CheckAdminRole::class);

// Customer Route
Route::get('edit_profile', [UserController::class, 'edit_profile'])->name('edit_profile')->middleware(CheckCustomerRole::class);
Route::post('profile_update', [UserController::class, 'profile_update'])->name('profile_update')->middleware(CheckCustomerRole::class);
Route::get('change_password', [UserController::class, 'change_password'])->name('change_password')->middleware(CheckCustomerRole::class);
Route::post('password_update', [UserController::class, 'password_update'])->name('password_update')->middleware(CheckCustomerRole::class);

//DepartmentStaff Route
Route::get('edit_profile', [UserController::class, 'edit_profile'])->name('edit_profile')->middleware(CheckDepartmentStaffRole::class);
Route::post('profile_update', [UserController::class, 'profile_update'])->name('profile_update')->middleware(CheckDepartmentStaffRole::class);
Route::get('change_password', [UserController::class, 'change_password'])->name('change_password')->middleware(CheckDepartmentStaffRole::class);
Route::post('password_update', [UserController::class, 'password_update'])->name('password_update')->middleware(CheckDepartmentStaffRole::class);

//admin middleware
public function handle(Request $request, Closure $next)
{
    if (auth()->user()->role == 1) {
        return $next($request);
    }
    return redirect('error/404'); 
}
//customer middleware
public function handle(Request $request, Closure $next)
{
    if (auth()->user()->role == 4) {
        return $next($request);
    }
    return redirect('error/404');
}
//Department middleware
public function handle(Request $request, Closure $next)
{
    if (auth()->user()->role == 2) {
        return $next($request);
    }
    return redirect('error/404');
}

CodePudding user response:

You can use only one middleware to check routes like:

//middleware
public function handle(Request $request, Closure $next)
{
    if (in_array(auth()->user()->role, [1, 2, 3])) {
        return $next($request);
    }
    return redirect('error/404'); 
}

And routes:

Route::get('edit_profile', [UserController::class, 'edit_profile'])->name('edit_profile')->middleware(CheckRole::class);
Route::post('profile_update', [UserController::class, 'profile_update'])->name('profile_update')->middleware(CheckRole::class);
Route::get('change_password', [UserController::class, 'change_password'])->name('change_password')->middleware(CheckRole::class);
Route::post('password_update', [UserController::class, 'password_update'])->name('password_update')->middleware(CheckRole::class);
  • Related