У меня есть автоматическое перенаправление на странице входа при доступе к маршруту /user/*. Мне нужно отображать флэш-сообщения при перенаправлении на страницу входа.
Я читал что-то о прослушивателях событий, но мне нужен реальный пример, чтобы реализовать это.
Я пытался:
services:
listener.requestresponse:
class: SciForum\Version2Bundle\EventListener\ExceptionListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
И мой ExceptionListener
class ExceptionListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
// You get the exception object from the received event
$exception = $event->getException();
$message = sprintf(
'My Error says: %s with code: %s',
$exception->getMessage(),
$exception->getCode()
);
// Customize your response object to display the exception details
$response = new Response();
$response->setContent($message);
// HttpExceptionInterface is a special type of exception that
// holds status code and header details
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers->replace($exception->getHeaders());
} else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Send the modified response object to the event
$event->setResponse($response);
}
}
Но исключение составляет более новый бросок, когда есть автоматическое перенаправление.