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(
        'AppBundle:Article:recentArticles',
        { 'max': 3 }
    )) }}
</div>

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:
http://symfony.com/doc/current/book/http_cache.html#edge-side-includes

Still not convinced?

Contact Us and tell what You need.

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

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

    Tadeusza Borowskiego 2 / 15B WARSAW, POLAND

  • Email

    contact@nextima.com

  • Phone

    (+48) 501 32 40 41