Redirect routes to separate controllers for same url pattern?

All we need is an easy explanation of the problem, so here it is.

In my routes file "web.php", I’m having below routes.

//Backend Routes
Route::group(['middleware'=>'auth', 'prefix' => 'dashboard'], function () {
    Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/leads', [AllLeadsController::class],'index')->name('web-leads');
    Route::resource('/news', NewsController::class);
    Route::resource('/page', PageController::class);
    Route::resource('/services', ServicesController::class);
    Route::resource('/blog', BlogController::class);

Route::get('/{slug}', [PageController::class, 'getPage'])->where('slug', '([A-Za-z0-9\-\/]+)')->name('link_view');

I’m trying to access pages with{slug} and dashboard with pattern but dashboard prefix routes are redirecting to PageController instead of dashboard controller.

I tried where clause as below but still dashboard routes mapping to PageController.

Route::get('/{slug}', [PageController::class, 'getPage'])->where(['slug','!=','login'],['slug','!=','dashboard'],['slug','=','([A-Za-z0-9\-\/]+)'])->name('link_view');

Please suggest a solution to the above issue.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

In Routes you can only use one where condition. So you have to make one regular expression to include all conditions.

Or you can just have dashboard and login routes before universal {slug}.
For example:

Route::get('/login', [...]);
Route::get('/dashboard', [...]);
Route::get('/{slug}', [PageController::class, 'getPage'])->name('link_view');

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply