Symfony2, check if an action is called by ajax or not Symfony2, check if an action is called by ajax or not symfony symfony

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


It's very easy!

Just add $request variable to your method as use. (For each controller)

<?phpnamespace YOUR\Bundle\Namespaceuse 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.

Source


First of all, note that getRequest() is deprecated, so get the request through an argument in your action methods.

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:

$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:

$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.