从模块转发到另一个 controller/action

Forward to another controller/action from module

我正在尝试找出在触发 MvcEvent::EVENT_DISPATCH_ERROR 时转发到另一个 controller/action 的最佳方式。

到目前为止,我在 bootstrap 模块中的内容是:

$eventManager = $event->getApplication()->getEventManager();
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($e) {
$routeMatch = new Router\RouteMatch(array('controller'=>'my-controller','action'=>'my-action');
}, 100);

我的问题是:

  1. 如何调度 $routeMatch?
  2. 这样做正确吗?

作为背景,我正在升级一个旧的 ZF1 应用程序,我在其中使用了前端插件来执行此操作。这是一个 cms 应用程序,当找不到匹配的路由时,cms 控制器用于将路由与数据库条目和 return 页面匹配。

谢谢

亚当

当用户想要访问其角色不允许的操作时,我就是这样做的。使用参数中的 $e:

$match = $e->getRouteMatch();
$match->setParam('controller', 'User\Controller\Account');
$match->setParam('action', 'denied');

希望对您有所帮助。

您需要使用新路线重新派发活动。

创建新的路由匹配后添加:

$newEvent = clone $e;
$e->stopPropagation(TRUE);
$newEvent->setRouteMatch($routeMatch);
$eventManager->trigger('dispatch', $newEvent);