1
votes

This is my routes :
I have Route Name Prefixes (admin) with namespace (Admin) for controller and prefix for url (users) , the code below works correctly but i want to use 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');
});

when i use the code below and open the urls like :
site.com/users/create and other urls ... it show me 404 Not Found Page!

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

Why is this is happening? ? how can i use resource like the first code that works?

2
use this command to check correctly route name php artisan route:listKingo
@Ivan when i use this command for resource it says : Class App\Http\Controllers\Admin\UserController does not exist My Class names have s like UsersController , should i rename my controllers in Singular form? all of my controllers name are pluralAmin Arjmand
Go to UserController and change namespace to App\Http\Controllers\Admin;Kingo

2 Answers

2
votes

When using a resource the resource such as users is used as prefix. Using prefix in a group is then no longer necessary. The following fixes the issue:

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

old:

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

without the updated resource:

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

Seems like you getting users/users/create right now

Remove the first prefix or change to admin, then you should have admin/users/create