我想从主页访问特定产品:
<a href="{{ path('property.show', {id: property.id, slug: property.slug}) }}">{{ property.title }}</a>
我在控制器中指定了路线
/**
* @Route("/property/{slug}-{id}", name="property.show", requirements={"slug": "[a-z0-9\-]*"})
* @param Property $property
* @return Response
*/
public function show(Property $property, string $slug): Response
{
if ($property->getSlug() !== $slug)
{
return $this->redirectToRoute('property.show', [
'id' => $property->getId(),
'slug' => $property->getSlug()
], 301);
}
$property = $this->repository->find($id);
return $this->render('property/show.html.twig', [
'property' => $property,
'current_menu' => 'properties'
]);
但是当我点击链接时,我得到一个错误“没有为“GET /my-first-property/property/-1”找到路由(来自“http://localhost:8000/”)”
我要生成的 URL 应该是 /property/my-first-property-1,我不明白为什么它不起作用。