Php 无法为命名路由生成URL
我试图生成包含两个参数的URL链接(这两个参数都不是必需的,但我这样做是为了练习)。我在Php 无法为命名路由生成URL,php,symfony,Php,Symfony,我试图生成包含两个参数的URL链接(这两个参数都不是必需的,但我这样做是为了练习)。我在discovercontroller /** * @Route("/rivers/{river_id}/discoverers/{id}", name="discoverer_show") * @Template */ public function showAction($river_id, $id){ $em = $this->getD
discovercontroller
/**
* @Route("/rivers/{river_id}/discoverers/{id}", name="discoverer_show")
* @Template
*/
public function showAction($river_id, $id){
$em = $this->getDoctrine()->getEntityManager();
$river = $em->getRepository('MyOwnBundle:River')->find($river_id);
if(!$river){
throw $this->createNotFoundException("no river with provided id");
}
$entity = $river->getDiscoverer();
return array('entity' => $entity);
}
正如您所看到的,传递了两个参数,河流id和发现者id(这很荒谬,但正如我所说的,实践…)。
在显示河流的动作(/rivers/1)中,我决定使用以下代码:
<a href="{{ path('discoverer_show', {'river_id': entity.id, 'id': entity.discoverer.id})}}"><p>{{entity.discoverer.name}}</p></a>
我不知道哪里出了问题,我提供了必要的参数并使用了我在控制器中定义的“discoverer_show”。如何正确呈现此链接?好的,我偶然发现了它。symfony2中的注释不能以tab开头 所以这件事是行不通的
/**
* @Route("/people")
*/
但这会很有魅力:
/**
* @Route("/people")
*/
一条建议:根本不要在源代码中使用制表符!使IDE用4个空格替换制表符。这可以帮你省去很多麻烦。。。标签在git中也表现不好
/**
* @Route("/people")
*/