src/EventListener/MyEventListener.php line 12

Open in your IDE?
  1. <?php 
  2. namespace App\EventListener;
  3. use Symfony\Component\HttpFoundation\Response;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. class MyEventListener
  8. {
  9.     public function onKernelException(ExceptionEvent $event)
  10.     {
  11.         // You get the exception object from the received event
  12.         $exception $event->getThrowable();
  13.         $message $this->TestBlockHTML ($exception->getMessage());
  14.        
  15.         /*
  16.         sprintf(
  17.             'My Error says: %s with code: %s',
  18.             $exception->getMessage(),
  19.             $exception->getCode()
  20.         );
  21. */
  22.         // Customize your response object to display the exception details
  23.         $response = new Response();
  24.         $response->setContent($message);
  25.         // HttpExceptionInterface is a special type of exception that
  26.         // holds status code and header details
  27.         if ($exception instanceof HttpExceptionInterface) {
  28.             $response->setStatusCode($exception->getStatusCode());
  29.             $response->headers->replace($exception->getHeaders());
  30.         } else {
  31.             $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  32.         }
  33.         // sends the modified response object to the event
  34.         $event->setResponse($response);
  35.     }
  36.     public function onKernelRequest(RequestEvent $event) {
  37.       
  38.     }
  39.     
  40.     public function onKernelRequestBlocker(RequestEvent $event) {
  41.     }
  42.     protected function TestBlockHTML ($replStr) {
  43.         return <<<HTML
  44.             <html>
  45.             <body><h1>{$replStr}</h1>
  46.             </body>
  47.             </html>
  48.         HTML;
  49.         }
  50. }