Home > Mobile >  Changing the order of routes in web.php file causes my application to throw a 404 error in Laravel 9
Changing the order of routes in web.php file causes my application to throw a 404 error in Laravel 9

Time:04-24

I am trying to make CRUD (Create, Read, Update and Destroy) operations on Category class in my Laravel website. So, I have defined the relevant routes as below:

//Routes for Categories
Route::get('categories',[CategoryController::class, 'index'])->name('categories');
Route::get('categories/{category}',[CategoryController::class, 'show'])->name('show_category');
Route::get('categories/create',[CategoryController::class, 'create'])->name('create_category');
Route::get('categories/edit/{category}',[CategoryController::class, 'edit'])->name('edit_category');
Route::get('categories/destroy',[CategoryController::class, 'destroy'])->name('destroy_category');
Route::post('categories/store',[CategoryController::class, 'store'])->name('store_category');
Route::put('categories/update/{category}',[CategoryController::class, 'update'])->name('update_category');

Then I have made a view file named create-category.blade.php and finally, in the relevant controller for the Category model, I have written the following code for create method:

public function create()
{
    $pageTitle = "Create New Category";
    return view('create-category', compact('pageTitle'));
}

Using the codes mentioned above and the following line of code in my categories.blade.php file, I receive a 404 error when I try to access my create-category.blade.php file.

<a  href="{{ route('create_category') }}">Create New Category</a>

Having said all these, when I change the order of lines where my routes have been defined the problem gets solved. By this I mean when I change the web.php file like below, everything is OK (Basically, I move the create's routes to the first line).

//Routes for Categories
Route::get('categories/create',[CategoryController::class, 'create'])->name('create_category');
Route::get('categories',[CategoryController::class, 'index'])->name('categories');
Route::get('categories/{category}',[CategoryController::class, 'show'])->name('show_category');
Route::get('categories/edit/{category}',[CategoryController::class, 'edit'])->name('edit_category');
Route::get('categories/destroy',[CategoryController::class, 'destroy'])->name('destroy_category');
Route::post('categories/store',[CategoryController::class, 'store'])->name('store_category');
Route::put('categories/update/{category}',[CategoryController::class, 'update'])->name('update_category');

Is there any reasons behind this?

CodePudding user response:

As @Marleen says you have to set most exact routes first. In your case it should look like

//Routes for Categories
Route::get('categories/edit/{category}',[CategoryController::class, 'edit'])->name('edit_category');
Route::put('categories/update/{category}',[CategoryController::class, 'update'])->name('update_category');
// ^ 3 segment routes are set first
Route::get('categories/create',[CategoryController::class, 'create'])->name('create_category');
Route::get('categories/destroy',[CategoryController::class, 'destroy'])->name('destroy_category');
Route::post('categories/store',[CategoryController::class, 'store'])->name('store_category');
// ^ 2 segment routes with exact match are set second
Route::get('categories/{category}',[CategoryController::class, 'show'])->name('show_category');
// ^ 2 segment routes with dynamic match is set third
Route::get('categories',[CategoryController::class, 'index'])->name('categories');
// ^ 1 segment route with exact match is set fourth
  • Related