Symfony2: Change rendered view with a listener

Viewed 11201

I would like to render different views in different context in my Symfony2 project. I'm using multiple routes for the same actions and I would like to render a different page (view) but with the same controller. For example I have:

@Route("/articles/show", name="articles_show")
@Route("/mobile/articles/show", name="mobile_articles_show")

Both routes are using the same action : ArticlesController:showAction(), but should render 2 differents templates (for mobile users and regulars ones).

show.html.twig
mobile.show.html.twig

I do not want to use a if statement or whatever in my controller, so I created a listener (similar to a preExecute function)

Here is a part or my config.yml that defines my listener

services:
    controller.pre_execute_listener:
        class: MyProject\MyBundle\Listener\ControllerListener
        arguments: ["@security.context", "@doctrine", "@router", "@session"]
        tags:- { name: kernel.event_listener, event: kernel.controller, method: preExecute }

I was thinking about doing something like that in the listener preExecute function:

if(substr($route,0,7) == 'mobile_'){
    $view = 'mobile.'.$view;
}

Unfortunately I cannot find a way to get $view or update the view "on the fly", just before it's rendered.

I hope my question is clear enough, thanks in advance, any idea is welcome :)

J.

4 Answers
Related