How to prefix “admin” to routes in Laravel?

不想你离开。 提交于 2019-12-11 18:48:28

问题


I have recently installed laravel 6 for a project.

unfortunately, the routes don't work properly.

for example i lead a user to a page with this route..

{{ route('user.edit', ['id' => $user->id]) }}.

It should go to admin/user/{user}/edit.

But it goes to an unknown route like this:

/admin/admin/user//edit?id=1 

my route :

Route::group(['namespace' => 'Admin', 'middleware' => ['auth', 'IsVerified'], 'prefix' => 'admin'], function () {
    route::resource('/user', 'UserController');
});

回答1:


Your route model binding is incorrect, change your href to this

{{ route('user.edit', ['user' => $user]) }}

Result:

/admin/user/1/edit

Calling Route::resource on a model returns this url for an edit route

+--------+-----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
| Domain | Method    | URI                        | Name             | Action                                                                 | Middleware                                      |
+--------+-----------+----------------------------+------------------+------------------------------------------------------------------------+-------------------------------------------------+
|        | GET|HEAD  | admin/user/{user}/edit     | user.edit        | App\Http\Controllers\Admin\UserController@edit                         | web,auth,IsVerified                             |

Note that the user model is used for the binding and then the ID is automatically fetched by the getRouteKeyName function which returns id by default




回答2:


  1. The default placeholder for a resource is the model 'user' not 'id'

{{ route('user.edit', ['id' => $user->id]) }}

would become:

{{ route('user.edit', $user) }}

  1. The double slash in your route is caused by the unecessary slash in

route::resource('/user', 'UserController');

which could just be:

route::resource('user', 'UserController');

  1. As for the double 'admin', perhaps that is being output in your view, as your namespace and grouping appears correct.


来源:https://stackoverflow.com/questions/58136586/how-to-prefix-admin-to-routes-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!