doudun2565 2014-09-08 15:50
浏览 48
已采纳

“无法为指定路由生成URL

Ive just started playing with symfony2 and im working on adding url etc. I cant seem to get my twig template to pick up on my function when passed its name using @Route. Any ideas why?

Controller:

/**
* @Route("/cube/{number}", name="get_cubed")
*/
public function indexAction($number)
{
    $cube = $number * $number * $number;
    return $this->render('NumberCubedBundle:Default:index.html.twig',
        array('number' => $number, 'cube' => $cube)
    );
}

My Twig File:

{% extends '::base.html.twig' %}
{% block title %}Cube Number Generator{% endblock %}
{% block body %}
    {{ number }}^3 = {{ cube }}
    <a href="{{ path('get_cubed', { 'number': 40 }) }}">Cube 40</a>
{% endblock %}

The Error:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "get_cubed" as such route does not exist.") in NumberCubedBundle:Default:index.html.twig at line 5. 

Any help would be massively appreciated. Thanks

  • 写回答

2条回答 默认 最新

  • dongwei2610 2014-09-08 15:54
    关注

    EDIT: why the annotation didn't work

    Most probably you didn't include Sensio's vendor bundle as explained in annotation routing and sensio framwork EXTRA bundle.

    The default routing mechanism uses the routing file (routing.yml) which does not need the extra bundle.

    The routing through annotations, on the other hand, are considered an additional feature that is not always desired and thus has been extracted to a separate and optional bundle.


    You need to configure the route in the routing file:

    app/config/routing.yml
    

    Define the route get_cubed using the standard Symfony 2 routing syntax.

    Much like this:

    get_cubed:
        path:      /cube/{number}
        defaults:  { _controller: NumberCubedBundle:Default:index }
        requirements:
            number: \d+
    

    Now you should be able to get the page with the route:

    .../app_dev.php/cube/40
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题