Laravel Route::resource 无法正常工作
Laravel Route::resource doesn't work correctly
这是我的路线:
我有 Route Name Prefixes (admin) with namespace (Admin) for controller 和 prefix for url (users) ,下面的代码工作正常但我想使用资源:
Route::prefix('users')->name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::get('', 'UsersController@index')->name('users.index');
Route::get('/create', 'UsersController@create')->name('users.create');
Route::post('/create', 'UsersController@store');//->name('admin.users.store');
Route::get('/edit/{user_id}', 'UsersController@edit')->name('users.edit');
Route::post('/edit/{user_id}', 'UsersController@update');//->name('admin.users.edit');
Route::get('/delete/{user_id}', 'UsersController@destroy')->name('users.delete');
});
当我使用下面的代码并打开 urls 时:
site.com/users/create 和其他 urls ... 它显示 404
找不到页面!
Route::prefix('users')->name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::resource('users', 'UserController');
});
为什么会这样? ?我怎样才能像第一个有效的代码那样使用资源?
使用资源时,users
等资源用作前缀。不再需要在组中使用前缀。以下解决了这个问题:
Route::name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::resource('users', 'UsersController');
});
旧:
| | GET|HEAD | users/users | admin.users.index | App\Http\Controllers\Admin\UsersController@index | web,auth |
| | POST | users/users | admin.users.store | App\Http\Controllers\Admin\UsersController@store | web,auth |
| | GET|HEAD | users/users/create | admin.users.create | App\Http\Controllers\Admin\UsersController@create | web,auth |
| | GET|HEAD | users/users/{user} | admin.users.show | App\Http\Controllers\Admin\UsersController@show | web,auth |
| | PUT|PATCH | users/users/{user} | admin.users.update | App\Http\Controllers\Admin\UsersController@update | web,auth |
| | DELETE | users/users/{user} | admin.users.destroy | App\Http\Controllers\Admin\UsersController@destroy | web,auth |
| | GET|HEAD | users/users/{user}/edit | admin.users.edit | App\Http\Controllers\Admin\UsersController@edit | web,auth |
+--------+-----------+-------------------------+-------------------------------+----------------------------------------------------+--------------+
没有更新的资源:
| | GET|HEAD | users | admin.users.index | App\Http\Controllers\Admin\UsersController@index | web,auth |
| | POST | users | admin.users.store | App\Http\Controllers\Admin\UsersController@store | web,auth |
| | GET|HEAD | users/create | admin.users.create | App\Http\Controllers\Admin\UsersController@create | web,auth |
| | GET|HEAD | users/{user} | admin.users.show | App\Http\Controllers\Admin\UsersController@show | web,auth |
| | PUT|PATCH | users/{user} | admin.users.update | App\Http\Controllers\Admin\UsersController@update | web,auth |
| | DELETE | users/{user} | admin.users.destroy | App\Http\Controllers\Admin\UsersController@destroy | web,auth |
| | GET|HEAD | users/{user}/edit | admin.users.edit | App\Http\Controllers\Admin\UsersController@edit | web,auth |
+--------+-----------+--------------------+-------------------------------+----------------------------------------------------+--------------+
看来你现在 users/users/create
去掉第一个前缀或者改成admin
,那么你应该有admin/users/create
这是我的路线:
我有 Route Name Prefixes (admin) with namespace (Admin) for controller 和 prefix for url (users) ,下面的代码工作正常但我想使用资源:
Route::prefix('users')->name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::get('', 'UsersController@index')->name('users.index');
Route::get('/create', 'UsersController@create')->name('users.create');
Route::post('/create', 'UsersController@store');//->name('admin.users.store');
Route::get('/edit/{user_id}', 'UsersController@edit')->name('users.edit');
Route::post('/edit/{user_id}', 'UsersController@update');//->name('admin.users.edit');
Route::get('/delete/{user_id}', 'UsersController@destroy')->name('users.delete');
});
当我使用下面的代码并打开 urls 时:
site.com/users/create 和其他 urls ... 它显示 404
找不到页面!
Route::prefix('users')->name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::resource('users', 'UserController');
});
为什么会这样? ?我怎样才能像第一个有效的代码那样使用资源?
使用资源时,users
等资源用作前缀。不再需要在组中使用前缀。以下解决了这个问题:
Route::name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
Route::resource('users', 'UsersController');
});
旧:
| | GET|HEAD | users/users | admin.users.index | App\Http\Controllers\Admin\UsersController@index | web,auth |
| | POST | users/users | admin.users.store | App\Http\Controllers\Admin\UsersController@store | web,auth |
| | GET|HEAD | users/users/create | admin.users.create | App\Http\Controllers\Admin\UsersController@create | web,auth |
| | GET|HEAD | users/users/{user} | admin.users.show | App\Http\Controllers\Admin\UsersController@show | web,auth |
| | PUT|PATCH | users/users/{user} | admin.users.update | App\Http\Controllers\Admin\UsersController@update | web,auth |
| | DELETE | users/users/{user} | admin.users.destroy | App\Http\Controllers\Admin\UsersController@destroy | web,auth |
| | GET|HEAD | users/users/{user}/edit | admin.users.edit | App\Http\Controllers\Admin\UsersController@edit | web,auth |
+--------+-----------+-------------------------+-------------------------------+----------------------------------------------------+--------------+
没有更新的资源:
| | GET|HEAD | users | admin.users.index | App\Http\Controllers\Admin\UsersController@index | web,auth |
| | POST | users | admin.users.store | App\Http\Controllers\Admin\UsersController@store | web,auth |
| | GET|HEAD | users/create | admin.users.create | App\Http\Controllers\Admin\UsersController@create | web,auth |
| | GET|HEAD | users/{user} | admin.users.show | App\Http\Controllers\Admin\UsersController@show | web,auth |
| | PUT|PATCH | users/{user} | admin.users.update | App\Http\Controllers\Admin\UsersController@update | web,auth |
| | DELETE | users/{user} | admin.users.destroy | App\Http\Controllers\Admin\UsersController@destroy | web,auth |
| | GET|HEAD | users/{user}/edit | admin.users.edit | App\Http\Controllers\Admin\UsersController@edit | web,auth |
+--------+-----------+--------------------+-------------------------------+----------------------------------------------------+--------------+
看来你现在 users/users/create
去掉第一个前缀或者改成admin
,那么你应该有admin/users/create