Laravel Route :: resource无法正常运行

时间:2019-09-05 07:46:48

标签: laravel laravel-5 routes laravel-resource

这是我的路线:
我有用于控制器的名称空间(Admin)的路由名称前缀(admin)和用于URL的前缀(users)的前缀,以下代码可以正常工作,但是我想使用resource:

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

当我使用以下代码并打开以下网址时:
site.com/users/create和其他网址...它向我显示404 找不到页面!

Route::prefix('users')->name('admin.')->namespace('Admin')->middleware(['auth'])->group(function () {
    Route::resource('users', 'UserController');
});

为什么会这样? ?我该如何使用像第一个有效的代码这样的资源?

2 个答案:

答案 0 :(得分:2)

使用资源时,诸如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     |
+--------+-----------+--------------------+-------------------------------+----------------------------------------------------+--------------+

答案 1 :(得分:1)

好像您现在users/users/create

删除第一个前缀或更改为admin,那么您应该拥有admin/users/create