Symfony2,检查一个动作是否被 ajax 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23911982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symfony2, check if an action is called by ajax or not
提问by Clément Andraud
I need, for each action in my controller, check if these actions are called by an ajax request or not.
我需要,对于控制器中的每个操作,检查这些操作是否由 ajax 请求调用。
If yes, nothing append, if no, i need to redirect to the home page.
如果是,则不附加任何内容,如果否,我需要重定向到主页。
I have just find if($this->getRequest()->isXmlHttpRequest()), but i need to add this verification on each action..
我刚刚找到if($this->getRequest()->isXmlHttpRequest()),但我需要在每个操作上添加此验证..
Do you know a better way ?
你知道更好的方法吗?
回答by R. Canser Yanbakan
It's very easy!
这很容易!
Just add $request variable to your method as use. (For each controller)
只需将 $request 变量添加到您的方法中即可。(对于每个控制器)
<?php
namespace YOUR\Bundle\Namespace
use Symfony\Component\HttpFoundation\Request;
class SliderController extends Controller
{
public function someAction(Request $request)
{
if($request->isXmlHttpRequest()) {
// Do something...
} else {
return $this->redirect($this->generateUrl('your_route'));
}
}
}
If you want to do that automatically, you have to define a kernel request listener.
如果要自动执行此操作,则必须定义内核请求侦听器。
回答by Pierre de LESPINAY
For a reusable technique, I use the following from the base template
对于可重用的技术,我使用基本模板中的以下内容
{# app/Resources/views/layout.html.twig #}
{% extends app.request.xmlHttpRequest
? '::ajax-layout.html.twig'
: '::full-layout.html.twig' %}
So all your templates extending layout.html.twigcan automatically be stripped of all your standard markup when originated from Ajax.
因此,layout.html.twig当源自 Ajax 时,所有扩展的模板都可以自动剥离所有标准标记。
回答by tomazahlin
First of all, note that getRequest() is deprecated, so get the request through an argument in your action methods.
首先,请注意 getRequest() 已弃用,因此请通过操作方法中的参数获取请求。
If you dont want to polute your controller class with the additional code, a solution is to write an event listener which is a service.
如果你不想用额外的代码污染你的控制器类,一个解决方案是编写一个事件侦听器,它是一个服务。
You can define it like this:
你可以这样定义它:
services:
acme.request.listener:
class: Acme\Bundle\NewBundle\EventListener\RequestListener
arguments: [@request_stack]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onRequestAction }
Then in the RequestListener class, make a onRequestAction() method and inject request stack through the constrcutor. Inside onRequestAction(), you can get controller name like this:
然后在 RequestListener 类中,创建一个 onRequestAction() 方法并通过构造函数注入请求堆栈。在 onRequestAction() 中,您可以像这样获取控制器名称:
$this->requestStack->getCurrentRequest()->get('_controller');
It will return the controller name and action (I think they are separated by :). Parse the string and check if it is the right controller. And if it is, also check it is XmlHttpRequest like this:
它将返回控制器名称和操作(我认为它们由 : 分隔)。解析字符串并检查它是否是正确的控制器。如果是,还要检查它是否是 XmlHttpRequest,如下所示:
$this->requestStack->getCurrentRequest()->isXmlHttpRequest();
If it is not, you can redirect/forward.
如果不是,您可以重定向/转发。
Also note, that this will be checked upon every single request. If you check those things directly in one of your controllers, you will have a more light-weight solution.
另请注意,这将在每个请求时进行检查。如果您直接在其中一个控制器中检查这些内容,您将拥有一个更轻量级的解决方案。

