如何为"admin"添加前缀到拉拉韦尔(Laravel)的路线? [英] How to prefix "admin" to routes in Laravel?

查看:200
本文介绍了如何为"admin"添加前缀到拉拉韦尔(Laravel)的路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近为一个项目安装了laravel 6.

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]) }}.

应该转到admin/user/{user}/edit.

但是它转到了一条未知的路线:

But it goes to an unknown route like this:

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

我的路线:

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

推荐答案

您的路由模型绑定不正确,请将href更改为此

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

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

结果:

/admin/user/1/edit

在模型上调用Route::resource会返回此网址以获取修改路线

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                             |

请注意,user模型用于绑定,然后getRouteKeyName函数自动获取ID,该函数默认返回id

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

这篇关于如何为"admin"添加前缀到拉拉韦尔(Laravel)的路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆