Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 : customize error pages for different bundles

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 ?

like image 716
Boun Avatar asked Jun 20 '12 10:06

Boun


1 Answers

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
    }
  }
}

And register it

//services.yml
kernel.listener.yourlistener:
  class: Your\MainBundle\YourExceptionListener
  tags:
    - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
like image 113
Peter Bailey Avatar answered Nov 05 '22 02:11

Peter Bailey