dongqian6554 2014-08-24 19:21
浏览 35

无法为命名路由生成URL

I'm trying to generate link to URL which contains two parameters (both of those parameters are not really necessary but I do it for practice). I created custom showAction in 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);
    }

As you can see two parameters are passed, id of the river and id of the discoverer (which is absurd but as I said, practice...). In show action of a river (/rivers/1) I decided to put following code:

<a href="{{ path('discoverer_show', {'river_id': entity.id, 'id': entity.discoverer.id})}}"><p>{{entity.discoverer.name}}</p></a>

Note that 'entity' is a river here, and river has a discoverer. Unfortunatelly, when I try to render this action, I get error which tells me that:

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.

I dont have a clue what is wrong, I provided both necessary parameters and used "discoverer_show" which I defined in my controller. How to correctly render this link?

  • 写回答

2条回答 默认 最新

  • donglei1699 2014-08-24 20:05
    关注

    Ok, by accident i figured it out. Turns out annotations in symfony2 CANNOT begin with tab.

    So this thing right here is NOT going to work

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

    But this will work like a charm:

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

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据