使用事件侦听器重定向所有“未找到路由 404 Not Found - NotFoundHttpException"; [英] Redirect with Event Listener for all "No route found 404 Not Found - NotFoundHttpException"
问题描述
如何在事件侦听器中触发重定向到特定路由器?
How can I trigger redirect to a specific router in event listener?
有很多例子,但我找不到一个关于GetResponseForExceptionEvent"的例子.例如,当我将 @roter
作为参数传递时 $this->router....
似乎不起作用.
There are many examples but I couldn't find one for "GetResponseForExceptionEvent". For exampe, when I pass @roter
as an argument $this->router....
doesn't seem to be working so on.
我检查了这些,但我可能错过了一些东西:
I checked these but I probably missed something:
- 显示特定路由而不是错误页面 (404) - Symfony2
- symfony2:挂钩 NotFoundHttpException 以进行重定向
- Symfony2 事件监听器重定向?
- Symfony2:在页面加载事件侦听器中获取路由
- 如果用户已登录则重定向
service.yml
services:
kernel.listener.kernel_request:
class: BookingAdminBundleEventListenerErrorRedirect
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
事件监听器:
namespace BookingAdminBundleEventListener;
use SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
class ErrorRedirect
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof NotFoundHttpException) {
// redirect to '/' router or '/error'
//$event->setResponse(...........);
}
}
}
推荐答案
您有 2 个选择:
- 使用事件监听器
- 使用匹配所有不存在的路由并在控制器中触发操作的路由.
让我知道这是否有效.
注意:查看Symfony2 事件侦听器重定向
1 将路由器传递给您的事件侦听器:
1 Pass the router to your event listener:
kernel.listener.kernel_request:
class: BookingAdminBundleEventListenerErrorRedirect
arguments:
router: "@router"
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
2 使用监听器内的路由器重定向用户:
2 Use the router within the listener to redirect users:
namespace BookingAdminBundleEventListener;
use SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyBundleFrameworkBundleRoutingRouter;
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:
Option 2
You can create a special route that will match all routes that don't exist; You can then handle the redirect the way you want within the controller of your choice, here PageNotFoundController:
1 在 app/config/routing.yml 末尾
1 At the end of app/config/routing.yml
pageNotFound:
pattern: /{path}
defaults: { _controller: AcmeDemoBundle:PageNotFound:pageNotFound, path: '' }
requirements:
path: .*
2
<?php
namespace AcmeDemoBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SymfonyComponentHttpFoundationJsonResponse;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
class PageNotFoundController extends Controller
{
public function pageNotFoundAction()
{
// redirect the way you want
}
}
这篇关于使用事件侦听器重定向所有“未找到路由 404 Not Found - NotFoundHttpException";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!