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

前端 未结 3 1032
無奈伤痛
無奈伤痛 2021-01-07 16:45

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

3条回答
  •  臣服心动
    2021-01-07 17:38

    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

提交回复
热议问题