Laravel 带有 id 和名称的路由
Laravel route with id and name
我这样生成 URL:
URL::action('FieldsController@show',['id' => $field->id, 'head' => cleanUrl($field->head)])
在我的路线中我有:
Route::get('/field/{head}-{id}', 'FieldsController@show');
它不起作用,只有当我像这样把 ID 放在第一位,然后将 HEAD 放在第二位时:
Route::get('/field/{id}-{head}', 'FieldsController@show');
有人有想法吗?我需要在 URL
中的 HEAD 之后输入 ID
你不能像
那样做路由
{head}-{id}
您需要这样做:
Route::get('/field/{head}/{id}', 'FieldsController@show');
然后在你的 show()
函数中你可以自己组合它们:
function show($head, $id)
{
$var = $head.'-'.$id;
// do whatever you want with $var here
}
我是这样使用的,效果很好:
Route::get('/blog/{slug}-{article}', [App\Http\Controllers\BlogController::class, 'article'])->name('blog.article')->where(['slug' => '[\w]+[^\d]+', 'article' => '[\d]+$']);
我这样生成 URL:
URL::action('FieldsController@show',['id' => $field->id, 'head' => cleanUrl($field->head)])
在我的路线中我有:
Route::get('/field/{head}-{id}', 'FieldsController@show');
它不起作用,只有当我像这样把 ID 放在第一位,然后将 HEAD 放在第二位时:
Route::get('/field/{id}-{head}', 'FieldsController@show');
有人有想法吗?我需要在 URL
中的 HEAD 之后输入 ID你不能像
那样做路由{head}-{id}
您需要这样做:
Route::get('/field/{head}/{id}', 'FieldsController@show');
然后在你的 show()
函数中你可以自己组合它们:
function show($head, $id)
{
$var = $head.'-'.$id;
// do whatever you want with $var here
}
我是这样使用的,效果很好:
Route::get('/blog/{slug}-{article}', [App\Http\Controllers\BlogController::class, 'article'])->name('blog.article')->where(['slug' => '[\w]+[^\d]+', 'article' => '[\d]+$']);