Symfony2:为不同的包定制错误页面

Bou*_*oun 13 bundles symfony

我有几个捆绑包,我想知道是否可以为每个捆绑包定制自己的错误页面.

我阅读了食谱,示例只显示了所有包的通用自定义页面.

有没有办法覆盖每个包的异常过程?

Pet*_*ley 5

监听器本身必须检测到 - 我不知道为单个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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

注册它

//services.yml
kernel.listener.yourlistener:
  class: Your\MainBundle\YourExceptionListener
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Run Code Online (Sandbox Code Playgroud)