My issue is, even if I added route in web.php then also it gives me 404 not found.
My route file as below.
<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersStudentController;
Route::get('/students', [StudentController::class, 'index'])->name('students.index');
Route::get('students/export/excel', [StudentController::class, 'exportExcel'])->name('students.export.excel');
Route::get('students/export/pdf', [StudentController::class, 'exportPdf'])->name('students.export.pdf');
Route::get('students/{student}', [StudentController::class, 'show'])->name('students.show');
Route::get('/students/create', [StudentController::class, 'create'])->name('students.create');
Route::post('/students/store', [StudentController::class, 'store'])->name('students.store');
If I add my route
Route::get('/students/create', [StudentController::class, 'create'])->name('students.create');
below the route
Route::get('students/{student}', [StudentController::class, 'show'])->name('students.show');
it gives me 404 and if I add my route above the
Route::get('students/{student}', [StudentController::class, 'show'])->name('students.show');
it works fine.
How is it possible? Can anyone explain this or provide the some link so I can understand the how it works?
Thanks in Advance. 🙂
2
Answers
I fixed this issue with below code.
I added the where() in particular route for solve route conflict. Now If, I move route above the
Route::get('/students/create', [StudentController::class, 'create'])->name('students.create');
route it will identify and work easily.Now my route (web.php) file looks like below.
The route Route::get(‘students/{student}’, …) is a dynamic route that matches any URL segment after students/. For example:
/students/123
/students/xyz
This means when the route Route::get(‘students/{student}’, …) is defined before Route::get(‘/students/create’, …), Laravel tries to match /students/create with students/{student}. It interprets create as the {student} parameter, which likely causes the controller method or validation to fail.
To avoid conflicts between static and dynamic routes, always define specific routes before dynamic ones.