无法为命名路由生成URL

时间:2014-08-24 19:21:27

标签: php symfony

我正在尝试生成包含两个参数的URL链接(这两个参数都不是必需的,但我是为了练习而做的)。我在showAction

中创建了自定义DiscovererController
   /**
    *   @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>

请注意,'entity'是河流,河流有发现者。不幸的是,当我尝试渲染这个动作时,我得到的错误告诉我:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "discoverer_show" as such route does not exist.") in /path/to/project/src/My/OwnBundle/Resources/views/River/show.html.twig at line 9.

我不知道出了什么问题,我提供了两个必要的参数,并使用了我在控制器中定义的“discoverer_show”。如何正确呈现此链接?

2 个答案:

答案 0 :(得分:1)

一条建议:根本不要在源代码中使用标签!使您的IDE用4个空格替换制表符。这可以为你节省很多麻烦......标签在git中也表现不佳。

答案 1 :(得分:0)

好吧,偶然的我想通了。在symfony2中输出注释不能以tab开头。

所以这里的事情不会起作用

/**
*   @Route("/people")
*/

但这会像魅力一样:

/**
* @Route("/people")
*/
相关问题