예외가 발생할 때 커널이 전달하는 kernel.exception
이벤트를 수신하는 event listener을 생성하려고합니다.
그런 다음 예외가 인스턴스 NotFoundHttpException
인 경우 수신기 내부를 검사하고, 그렇다면 리스너 내부를 선택하여 원하는 페이지로 리디렉션합니다.
<?php
// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AcmeExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getException();
if ($event->getException() instanceof NotFoundHttpException) {
$response = new RedirectResponse($url);
$event->setResponse($response);
}
}
}
물론, 당신은 이벤트 리스너를 등록해야합니다 :
은 여기 exemple입니다. 그것은 단지 서비스 일 뿐이므로 보통 때처럼 등록해야합니다.
# app/config/config.yml
services:
kernel.listener.your_listener_name:
class: Acme\DemoBundle\EventListener\AcmeExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
당신은 여기에서 기쁨을 발견 할 것이다 -> http://stackoverflow.com/questions/15983082/show-a-specific-route-instead-of-error-page-404-symfony2 – Healkiss