使用侦听器更改渲染视图

使用侦听器更改渲染视图

本文介绍了Symfony2:使用侦听器更改渲染视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Symfony2项目中的不同上下文中呈现不同的视图.我对同一操作使用了多个路由,并且我想使用相同的控制器呈现不同的页面(视图).例如,我有:

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")

两条路线都使用相同的操作:ArticlesController:showAction(),但应呈现2个不同的模板(适用于移动用户和常规用户).

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

我不想在控制器中使用if语句或其他内容,因此我创建了一个侦听器(类似于preExecute函数)

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

这是我的 config.yml 的一部分,它定义了我的监听器

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 }

我正在考虑在监听器 preExecute函数中做类似的事情:

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

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

不幸的是,我无法找到一种方法来获取 $ 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.

推荐答案

这是解决方案:

首先,我必须听 kernel.view ,而不是kernel.controller.

First I have to listen to kernel.view, not kernel.controller.

然后,我使用" @模板"服务(感谢Marko Jovanovic的提示)

Then I use the "@templating" service (Thanks Marko Jovanovic for the hint)

这是我的新config.yml:

So here is my new config.yml:

services:
    controller.pre_execute_listener:
        class: MyProject\MyBundle\Listener\ControllerListener
        arguments: ["@templating"]
        tags:
                - { name: kernel.event_listener, event: kernel.view, method: preExecute }

最后是我的监听器 preExecute函数

public function preExecute(\Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event){
    //result returned by the controller
    $data = $event->getControllerResult();

    /* @var $request  \Symfony\Component\HttpFoundation\Request */
    $request =  $event->getRequest();
    $template = $request->get('_template');
    $route = $request->get('_route');

    if(substr($route,0,7) == 'mobile_'){
        $newTemplate = str_replace('html.twig','mobile.html.twig',$template);

        //Overwrite original template with the mobile one
        $response = $this->templating->renderResponse($newTemplate, $data);
        $event->setResponse($response);
    }
}

希望这会有所帮助!

J.

这篇关于Symfony2:使用侦听器更改渲染视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:21