I need, for each action in my controller, check if these actions are called by an ajax request or not.
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..
Do you know a better way ?
The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.
It's very easy!
Just add $request variable to your method as use. (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.
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With