首頁>Program>source

如何在事件侦听器中觸發重定向到特定路由器?

有很多示例,但我找不到" GetResponseForExceptionEvent"的示例.例如,当我通過 @roter 作為論据 似乎没有這樣的工作。

我檢查了這些,但我可能錯過了一些东西:

$this->router....

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(...........); } } }
最新回復
  • 12天前
    1 #

    You have 2 options:

      Use an Event Listener

      Use a route that matches all the routes that don't exist and trigger an action in a controller.

    让我知道是否可行。


    選項1

    註意:了解事件监听器的Symfony2重定向

    1將路由器傳遞到事件监听器:

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

    2使用侦听器中的路由器重定向使用者:

    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);
            }
        }
    }
    

    選項2

    您可以建立一个特殊的路由,以匹配所有不存在的路由; 然後,您可以在您選擇的控製器(此處為PageNotFoundController)中以所需的方式處理重定向:

    1在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
        }
    }
    

  • java:正確關闭SSLSocket
  • node.js:如何將4个或更多表順序連線?