<?php
namespace App\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
class ExceptionListener implements EventSubscriberInterface {
protected $twig;
public function __construct(Environment $twig) {
$this->twig = $twig;
}
public function onKernelException(ExceptionEvent $event) {
$exception = $event->getThrowable();
if ($exception instanceof NotFoundHttpException) {
$response = new Response();
$response->setContent($this->twig->render('shared/404.html.twig'));
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
$event->setResponse($response);
}
}
public static function getSubscribedEvents() {
return array(
KernelEvents::EXCEPTION => 'onKernelException'
);
}
}