1

这是我的路线:
我有路由名称前缀(管理员),控制器的名称空间(管理员)和 url(用户)的前缀,下面的代码可以正常工作,但我想使用资源:

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 Not Found Page!

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

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

4

2 回答 2

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     |
+--------+-----------+--------------------+-------------------------------+----------------------------------------------------+--------------+

于 2019-09-05T07:54:39.040 回答
1

好像你users/users/create现在开始了

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

于 2019-09-05T07:54:58.757 回答