Forwarding using Event Listener for all "404 route not found Not Found - NotFoundHttpException"

How can I call a redirect to a specific router in an event listener?

There are many examples, but I could not find them for "GetResponseForExceptionEvent". For example, when I pass @roter as an argument to $this->router.... it doesn't seem to work like that.

I checked them, but I probably missed something:

service.yml

 services: kernel.listener.kernel_request: class: Booking\AdminBundle\EventListener\ErrorRedirect tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } 

Event Listener:

 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(...........); } } } 
+5
source share
1 answer

You have 2 options:

  • Use Event Listener
  • Use a route that matches all routes that do not exist and trigger the action in the controller.

Let me know if this works.


Option 1

Note. See Symfony2 redirect for event listener

1 Pass your event listener to the router:

 kernel.listener.kernel_request: class: Booking\AdminBundle\EventListener\ErrorRedirect arguments: router: "@router" tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } 

2 Use a router inside the listener to redirect users:

 namespace Booking\AdminBundle\EventListener; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Bundle\FrameworkBundle\Routing\Router; class ErrorRedirect { /** * Holds Symfony2 router * *@var Router */ protected $router; /** * @param Router */ public function __construct(Router $router) { $this->router = $router; } public function onKernelException(GetResponseForExceptionEvent $event) { $exception = $event->getException(); if ($exception instanceof NotFoundHttpException) { /** Choose your router here */ $route = 'route_name'; if ($route === $event->getRequest()->get('_route')) { return; } $url = $this->router->generate($route); $response = new RedirectResponse($url); $event->setResponse($response); } } } 

Option 2

You can create a special route that will correspond to all routes that do not exist; Then you can handle the redirect as you want in the controller of your choice, here is PageNotFoundController:

1 At the end of app / config / routing.yml

 pageNotFound: pattern: /{path} defaults: { _controller: AcmeDemoBundle:PageNotFound:pageNotFound, path: '' } requirements: path: .* 

2

 <?php namespace Acme\DemoBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class PageNotFoundController extends Controller { public function pageNotFoundAction() { // redirect the way you want } } 
+31
source

Source: https://habr.com/ru/post/1205084/


All Articles