带有 ID 和名称的 Laravel 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29918781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:27:56  来源:igfitidea点击:

Laravel route with id and name

laravellaravel-4routes

提问by Ale? Chromec

I generate URL like this:

我生成这样的网址:

URL::action('FieldsController@show',['id' => $field->id, 'head' => cleanUrl($field->head)])

In my routes I have:

在我的路线中,我有:

Route::get('/field/{head}-{id}', 'FieldsController@show');

And it dont work, only when I put ID first and HEAD second like this:

它不起作用,只有当我像这样把 ID 放在第一位,然后把 HEAD 放在第二位时:

Route::get('/field/{id}-{head}', 'FieldsController@show');

Anyone have ideas? I need to have ID after HEAD in URL

有人有想法吗?我需要在 URL 中的 HEAD 之后有 ID

回答by Laurence

You cant do routing like

你不能做路由

{head}-{id}

You need to do this:

你需要这样做:

Route::get('/field/{head}/{id}', 'FieldsController@show');

Then in your show()function you can combine them yourself:

然后在你的show()函数中你可以自己组合它们:

function show($head, $id)
{
     $var = $head.'-'.$id;
     // do whatever you want with $var here
}