Symfony2:针对不同捆绑包定制错误页面 [英] Symfony2 : customize error pages for different bundles

查看:105
本文介绍了Symfony2:针对不同捆绑包定制错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个捆绑软件,我想知道是否可以为每个捆绑软件自定义他们自己的错误页面

I have several bundles and I'd like to know if it is possible to customize for each bundle their own error pages.

我阅读了食谱,这些示例仅显示了所有捆绑软件的通用自定义页面。

I read the cookbook and the examples show only a generic customize page for all bundles.

是否有一种方法可以覆盖每个捆绑软件的异常处理?

Is there a way to override the exception process for each bundle ?

推荐答案

侦听器本身必须检测到这一点-我不知道有什么方法可以为单个分发包指定侦听器。

The listener itself would have to detect that - I'm not aware of any way to specify a listener for a single bundle.

<?

namespace Your\MainBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

class YourExceptionListener
{
  public function onKernelException(GetResponseForExceptionEvent $event)
  {
    $exception = $event->getException();
    $namespace = new \ReflectionObject( $event->getController() )->getNamespaceName();

    switch ( $namespace )
    {
      case 'Acme\\DemoBundle':
        // do whatever with $exception here
        break;
      case 'Some\\OtherBundle':
        // do whatever with $exception here
        break;
      case 'Your\\MainBundle':
        // do whatever with $exception here
        break;
      default;
        // default
    }
  }
}

并注册

//services.yml
kernel.listener.yourlistener:
  class: Your\MainBundle\YourExceptionListener
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

这篇关于Symfony2:针对不同捆绑包定制错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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