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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程