Skip to content
Advertisement

Redirect routes to separate controllers for same url pattern?

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::resource('/main-menu',MainMenuController::class);
});

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

I’m trying to access pages with domainname.in/{slug} and dashboard with domainname.in/dashboard 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.

Advertisement

Answer

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');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement