更改 web.php 文件中的路由顺序导致我的应用程序在 Laravel 9 中抛出 404 错误
Changing the order of routes in web.php file causes my application to throw a 404 error in Laravel 9
我正在尝试对我的 Laravel 网站中的类别 class 进行 CRUD(创建、读取、更新和销毁)操作。所以,我定义了相关的路线如下:
//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');
然后我创建了一个名为 create-category.blade.php
的视图文件,最后,在类别模型的相关控制器中,我为 create
方法编写了以下代码:
public function create()
{
$pageTitle = "Create New Category";
return view('create-category', compact('pageTitle'));
}
使用上述代码和我的 categories.blade.php
文件中的以下代码行,当我尝试访问我的 create-category.blade.php
文件时收到 404 错误。
<a class="nav-link" href="{{ route('create_category') }}">Create New Category</a>
综上所述,当我更改已定义路线的行的顺序时,问题就解决了。我的意思是,当我像下面这样更改 web.php
文件时,一切正常(基本上,我将创建的路由移动到第一行)。
//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');
这背后有什么原因吗?
正如@Marleen 所说,您必须首先设置最准确的路线。
在您的情况下,它应该看起来像
//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
对于 CRUD 操作,还有一个 Route::resource,节省了一些行和顺序问题
我为您推荐的代码:
Route::resource('categories', CategoryController::class);
我正在尝试对我的 Laravel 网站中的类别 class 进行 CRUD(创建、读取、更新和销毁)操作。所以,我定义了相关的路线如下:
//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');
然后我创建了一个名为 create-category.blade.php
的视图文件,最后,在类别模型的相关控制器中,我为 create
方法编写了以下代码:
public function create()
{
$pageTitle = "Create New Category";
return view('create-category', compact('pageTitle'));
}
使用上述代码和我的 categories.blade.php
文件中的以下代码行,当我尝试访问我的 create-category.blade.php
文件时收到 404 错误。
<a class="nav-link" href="{{ route('create_category') }}">Create New Category</a>
综上所述,当我更改已定义路线的行的顺序时,问题就解决了。我的意思是,当我像下面这样更改 web.php
文件时,一切正常(基本上,我将创建的路由移动到第一行)。
//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');
这背后有什么原因吗?
正如@Marleen 所说,您必须首先设置最准确的路线。 在您的情况下,它应该看起来像
//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
对于 CRUD 操作,还有一个 Route::resource,节省了一些行和顺序问题
我为您推荐的代码:
Route::resource('categories', CategoryController::class);