Symfony2 自定义错误异常监听器 - 渲染模板或传递给控制器 [英] Symfony2 Custom Error Exception Listener - Rendering templates or passing to a controller

查看:13
本文介绍了Symfony2 自定义错误异常监听器 - 渲染模板或传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm trying to work out the best way of handling custom error pages within Symfony2.This includes 500 and 404's etc.

I can create my own custom templates (error404.html.twig etc) and render these out fine, the issue is , the app requires a few variables be passed into the base template for the page to remain consistent. Using the built in exception handler results in required variables not being available.

I have successfully setup a custom Exception Event Listener, and registered it as a service:

namespace MyCoMyBundleListener;

use SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelExceptionHttpExceptionInterface;
use SymfonyComponentDependencyInjectionContainerInterface;
use SymfonyBundleTwigBundleTwigEngine;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;



class MyErrorExceptionListener
{

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // We get the exception object from the received event
        $exception = $event->getException();

        if($exception->getStatusCode() == 404)
        {

            //$engine = $this->container->get('templating');
            //$content = $engine->render('MyBundle:Default:error404.html.twig');    
            //return $response = new Response($content);

            /* Also Tried */
            //$templating = $this->container->get('templating');    
            //return $this->render('MyBundle:Default:index.html.twig');



            $response = new Response($templating->render('MyBundle:Exception:error404.html.twig', array(
                    'exception' => $exception
            )));

            $event->setResponse($response);

        }


    }
}

This doesn't work , as :$container is not available , meaning I cannot render my custom page.

So two questions really , is this the correct way to handle custom error pages, or should I pass the response off to a controller? If so , whats the best way of doing that?

If this is correct , how can I make the templating engine available within my Listener ?

解决方案

You should add into yours Listener

/**
 *
 * @var ContainerInterface
 */
private $container;

function __construct($container) {
    $this->container = $container;
}

How do you register your Listener? You should register Listener like Service

Like that

core.exceptlistener:
  class: %core.exceptlistener.class%
  arguments: [@service_container]
  tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException, priority: 200 }

The best way is don't use service_container. The best way is register only necessary services.

Like that

/**
 *
 * @var Twig_Environment
 */
private $twig;

function __construct($twig) {
    $this->twig = $twig;
}

这篇关于Symfony2 自定义错误异常监听器 - 渲染模板或传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆