是否有可能限制对AJAX只是一个途径? [英] Is it possible to restrict a route for AJAX only?
问题描述
是否有可能限制一个Symfony的2路线只有XHR请求?我想声明的路线,这是只有通过AJAX访问。
Is it possible to restrict a Symfony 2 route for XHR requests only? I want to declare routes, which are only accessible via AJAX.
我不希望把一些额外的线路到每个AJAX的特定操作,如认为:
I do not want to put some extra lines into each AJAX-specific-actions like that:
if ($request->isXmlHttpRequest()) {
// do something
} else {
// do something else
}
我要定义:
- 在一个规则的AJAX请求
- 在一个规则的GET / POST请求相同的URL
为了避开具有条件,比如上面。
in order to get around having conditions like above.
推荐答案
我知道这个问题是一个年纪大一点,但同时一个新途径,以实现这一目标被引入的中的Symfony 2.4
I know this question is a bit older but meanwhile a new way to achieve this was introduced in Symfony 2.4.
<一个href="http://symfony.com/doc/current/book/routing.html#completely-customized-route-matching-with-conditions">Completely定制路线匹配与细则
有关一个Ajax限制,它是这样的:
For an ajax restriction it would look like this:
contact:
path: /contact
defaults: { _controller: AcmeDemoBundle:Main:contact }
condition: "request.isXmlHttpRequest()"
也有可能在注释:
Also possible in Annotation:
/**
* ContactAction
*
* @Route("/contact", name="contact", condition="request.isXmlHttpRequest()")
*/
这篇关于是否有可能限制对AJAX只是一个途径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!