2014-10-20 12 views
5

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ì đó:

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

Trả lời

31

Bạn có 2 lựa chọn:

  • Sử dụng xử lý sự kiện
  • . Sử dụng một tuyến đường phù hợp với tất cả các tuyến đường không tồn tại và kích hoạt một hành động trong bộ điều khiển.

Hãy cho tôi biết nếu nó hoạt động.


Lựa chọn 1

Lưu ý: Hãy nhìn vào Symfony2 redirect for event listener

Vượt qua router để lắng nghe sự kiện của bạn:

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

Sử dụng các bộ định tuyến trong thính giả người dùng ct:

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

     } 
    } 
} 

Lựa chọn 2

Bạn có thể tạo ra một con đường đặc biệt mà sẽ phù hợp với tất cả các tuyến đường không tồn tại; Sau đó, bạn có thể xử lý chuyển hướng theo cách bạn muốn trong bộ điều khiển mà bạn chọn, tại đây PageNotFoundController:

Khi kết thúc ứng dụng/config/routing.yml

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

<?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 

    } 

} 
+4

Thật là một câu trả lời tuyệt vời! Cảm ơn nhiều. +++ 1 – BentCoder

+0

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? –

+0

Tùy chọn 2 hoạt động tốt. Cảm ơn! – TechyTee

Các vấn đề liên quan