Symfony 2 – How to Get parent Request in twig embedded controller

When we want to use twig functions like render controller:

<div id="sidebar">
    {{ render(controller(
        { 'max': 3 }
    )) }}

symfony create a sub-request, with it own params for embedded controller. Sometimes we made have to need data from original Request object (GET/POST/FILES). It’s very simple, and we don’t need to assigned it to variable.

In Request obejct there is dedicated method for this: createFromGlobals()

    public function recentArticlesAction(Request $request){
        $request =  $request->createFromGlobals();

Simple. But ok, wait…

Maybe we shouldn’t embed other controller in twig?

Yes, that’s right! Usually we should avoid embeding other controllers for performence reasons. But this is very usefull, when we working with ESI.

{{ render_esi(controller('AppBundle:News:latest', { 'maxPerPage': 5 })) }}

If you don’t know this method of caching, You should read it ASAP, and start using:

Still not convinced?

Contact Us and tell what You need.

We will prepare a competitive offer and send you examples of our previous projects.

  • Tweets not available at this time. Please Try again later
Toys we play with
Get in touch
  • Address:

    Nextima Sp. z o.o. ul. Terespolska 4 / 375 03813 WARSAW, POLAND

  • Email

  • Phone

    (+48) 501 32 40 41