src/MentalSchool/AppBundle/Listener/ExceptionListener.php line 21

Open in your IDE?
  1. <?php
  2. namespace  MentalSchool\AppBundle\Listener;
  3. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  6. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  7. class ExceptionListener
  8. {
  9.     protected $templating;
  10.     protected $kernel;
  11.     public function __construct(EngineInterface $templating$kernel)
  12.     {
  13.         $this->templating $templating;
  14.         $this->kernel $kernel;
  15.     }
  16.     public function onKernelException(GetResponseForExceptionEvent $event)
  17.     {
  18.         if ('prod' == $this->kernel->getEnvironment()){
  19.             $exception $event->getException();
  20.             // Customize your response object to display the exception details
  21.             $response = new Response();
  22.             $response->setContent(
  23.                 $this->templating->render(
  24.                     '::exception.html.twig',
  25.                     array('exception' => $exception)
  26.                 )
  27.             );
  28.             // HttpExceptionInterface is a special type of exception that
  29.             // holds status code and header details
  30.             if ($exception instanceof HttpExceptionInterface) {
  31.                 $response->setStatusCode($exception->getStatusCode());
  32.                 $response->headers->replace($exception->getHeaders());
  33.             } else {
  34.                 $response->setStatusCode(500);
  35.             }
  36.             // Send the modified response object to the event
  37.             $event->setResponse($response);
  38.         }
  39.     }