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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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');


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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x