你说得对,但你不能从逻辑上分开
/{slug}-{id}
如果你用的是破折号分隔的字符串。要处理此问题,只需分解块并选择最后一个:
// routes/web.php
Route::get('/{primarySlug}/{secondarySlugAndId}', [ExampleController::class, 'example']);
// ExampleController.php
public function example($primarySlug, $secondarySlugAndId){
$parts = collect(explode('-', $secondarySlugAndId));
$id = $parts->last();
$secondarySlug = $parts->slice(0, -1)->implode('-');
... // Do anything else you need to do
}
example.com/primary-slug/secondary-slug-99
,您将拥有以下变量:
dd($primarySlug, $secondarySlug, $id);
// "primary-slug"
// "secondary-slug"
// "99"
唯一的情况是如果你的
id