doutangqi4444 2014-02-08 08:18
浏览 48
已采纳

如何在Symfony2中获取控制器动作的路径?

I was practicing symfony2. And i got a problem:

I just made a simple twig page which posts data to an action of the controller, and there is a function(it works):

$.ajax({
    type: 'POST',
    url: "{{ path('my_test_account_register') }}",
    data: account,
    success: function(msg){
        alert("Succeed!");
    },
    error: function(XmlHttpRequest,textStatus, errorThrown){
        alert ("Failed! ");
    }
}

I don't want to add a annotation to every action of mine, howver. I wanted to replace url: "{{ path('my_test_account_register') }}" into url: "{{ path('MyTestBudnle:Account:register') }}" , but i failed.

My question is: how to specify the url of an action inside a controller? Maybe this is a silly question, and this is my first time to stackoverflow. So, nice to meet you guys!

  • 写回答

2条回答 默认 最新

  • dspld86684 2014-02-08 13:20
    关注

    The twig function path() you are (correctly) using, creates the url for a given route name and some optional parameters.

    A controller can have different routes (and thereby different urls) for the same action. Because of this, you cannot use the Bundle:Controller:Action notation, if you want to find out the url for a specific action. Instead, you have to use the route name.

    The difference between path() and render() is, that render() includes the response of a subrequest to a specific action into the template. Because this all happens internally, there is no need to generate an url for this. You can even use render() with actions, that have no route at all.

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器