简体   繁体   English

Symfony2,检查一个动作是否被 ajax 调用

[英]Symfony2, check if an action is called by ajax or not

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 ?你知道更好的方法吗?

It's very easy!这很容易!

Just add $request variable to your method as use.只需将 $request 变量添加到您的方法中即可。 (For each controller) (对于每个控制器)

<?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.如果要自动执行此操作,则必须定义内核请求侦听器。

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.twig can automatically be stripped of all your standard markup when originated from Ajax.因此,当源自 Ajax 时,所有扩展layout.html.twig的模板都可以自动剥离所有标准标记。

Source 来源

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.然后在 RequestListener 类中,创建一个 onRequestAction() 方法并通过构造函数注入请求堆栈。 Inside onRequestAction(), you can get controller name like this:在 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.如果您直接在其中一个控制器中检查这些内容,您将拥有一个更轻量级的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM