简体   繁体   English

Laravel使用参数无法按预期命名路由URL

[英]Laravel named route URL with parameter not working as expected

I'm trying to generate a url with parameter using laravel route helper, 我正在尝试使用laravel route helper生成带有参数的网址,

route('frontend.admin.categories.edit', $catRequest->id)

but this is the generated url 但这是生成的URL

http://localhost:8000/admin/categories/edit?1

I need a URL like this 我需要这样的网址

http://localhost:8000/admin/categories/edit/1

And this is my route 这是我的路线

 Route::get('admin/categories/edit/{id}', 'CategoryController@edit')
        ->name('admin.categories.edit');

What is the issue here? 这是什么问题?

You need to use an array for your params : 您需要为参数使用数组:

route('frontend.admin.categories.edit', ['id' => $catRequest->id])

If not, all params will be used as $_GET params. 否则,所有参数都将用作$ _GET参数。

You can specify the parameter(s) you are replacing by passing an associative array instead of a variable. 您可以通过传递关联数组而不是变量来指定要替换的参数。

Change, 更改,

route('frontend.admin.categories.edit', $catRequest->id)

To

route('frontend.admin.categories.edit', [
    'id' => $catRequest->id
]);

Edit #1 编辑#1

You are calling the wrong route, you named your route as admin.categories.edit in the definition yet you are calling frontend.admin.categories.edit within the helper function which is not the originally defined route. 您输入了错误的路由,在定义admin.categories.edit您的路由命名为admin.categories.edit ,但在辅助函数(不是最初定义的路由)中调用了frontend.admin.categories.edit So your code should be: 所以你的代码应该是:

route('admin.categories.edit', [
    'id' => $catRequest->id
]);

Reading Material 阅读材料

Named Routes 命名路线

Try to view your route list using php artisan route:list . 尝试使用php artisan route:list查看您的路线php artisan route:list If you found your route in list with correct Uri then see that you are not again specify that route with same Uri or same alias name in your route file. 如果您在列表中找到具有正确Uri的路由,那么您不会在路由文件中再次指定具有相同Uri或相同别名的路由。

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM