如何在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!
我正在练习symfony2。 我遇到了一个问题: p>
我刚刚创建了一个简单的twig页面,它将数据发布到控制器的一个动作,并且有一个函数(它可以工作): p>
$ .ajax({
type:'POST',
url:“{{path('my_test_account_register')}}”,
data:account,
success:function( msg){
alert(“成功!”);
},
错误:函数(XmlHttpRequest,textStatus,errorThrown){
alert(“失败!”);
}
}
code> pre>
我不想为我的每个动作添加注释,不管怎样。我想替换 url:“{{path('my_test_account_register')}} “ code>进入 url:”{{path('MyTestBudnle:Account:register')}}“ code>,但我失败了。 p>
我的问题是 :如何在控制器内指定动作的url?也许这是一个愚蠢的问题,这是我第一次使用stackoverflow。所以,很高兴见到你们! p>
div>
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.