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

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

问题描述

我正在尝试找出在 Symfony2 中处理自定义错误页面的最佳方法.这包括 500 和 404 等.

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

我可以创建自己的自定义模板(error404.html.twig 等)并将它们呈现得很好,问题是,应用程序需要将一些变量传递到页面的基本模板中以保持一致.使用内置的异常处理程序会导致所需的变量不可用.

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 MyCo\MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;



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

        }


    }
}

这不起作用,因为 :$container 不可用,这意味着我无法呈现我的自定义页面.

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 ?

推荐答案

你应该加入你的监听器

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

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

你如何注册你的监听器?你应该像服务一样注册监听器

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

就这样

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

最好的方法是不要使用 service_container.最好的方法是只注册必要的服务.

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

就这样

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

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

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

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