Symfony2: in Twig, pass Request with Render function

前端 未结 3 898
感情败类
感情败类 2020-12-17 15:27

I understand that using {% render() %} automatically forces a new request object to be sent, but im curious if theres a way to pass in the originating request a

相关标签:
3条回答
  • 2020-12-17 16:01

    If I understand correctly, you're looking for this:

    {% render "MyBundle:Controller:someAction" with { 'originalRequest' : app.request } %}
    
    0 讨论(0)
  • 2020-12-17 16:01

    use the render function as a result

    {{ render(controller('MyBundle:ControllerName:example', {'originalRequest': app.request})) }}
    

    and then in your controller

    public function exampleAction(Request $originalRequest)
    {
        // do something
    }
    
    0 讨论(0)
  • 2020-12-17 16:11

    Since Symfony 2.4 you can access the original request via the request_stack. This avoids the need to create a new method parameter.

    function exampleAction() {
      $request = $this->get('request_stack')->getMasterRequest();
      //do something
    }
    

    Use this carefully as it makes your sub-requests incompatible with ESIs/reverse proxies (where a sub-request is also a master request) http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

    0 讨论(0)
提交回复
热议问题