Làm cách nào để kích hoạt chuyển hướng đến một bộ định tuyến cụ thể trong trình xử lý sự kiện?Chuyển hướng với Trình xử lý sự kiện cho tất cả "Không tìm thấy tuyến đường 404 Không tìm thấy - NotFoundHttpException"
Có nhiều ví dụ nhưng tôi không thể tìm thấy một ví dụ cho "GetResponseForExceptionEvent". Đối với exampe, khi tôi vượt qua @roter
làm đối số $this->router....
dường như không hoạt động.
Tôi đã kiểm tra này, nhưng có lẽ tôi đã bỏ lỡ điều gì đó:
- Show a specific route instead of error page (404) - Symfony2
- symfony2: hook into NotFoundHttpException for redirection
- Symfony2 redirect for event listener?
- Symfony2: Getting Route in Page Load Event Listener
- Redirect if the user is logged in
service.yml
services:
kernel.listener.kernel_request:
class: Booking\AdminBundle\EventListener\ErrorRedirect
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
nghe sự kiện:
namespace Booking\AdminBundle\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ErrorRedirect
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
// redirect to '/' router or '/error'
//$event->setResponse(...........);
}
}
}
Thật là một câu trả lời tuyệt vời! Cảm ơn nhiều. +++ 1 – BentCoder
Nhưng điều này không chỉ hoạt động trên Dev chứ không phải trên môi trường Prod? –
Tùy chọn 2 hoạt động tốt. Cảm ơn! – TechyTee