[英]How to change action and call action/method from another controller?
我正在使用 Laminas 框架(laminas/mvc v3.1)。 在某些情况下,我需要针对给定路线调用与规定不同的操作,即向无权查看受限内容的用户显示默认页面。
为此,我调整了 Module.php 中的 onDispatch 方法,如下所示:
public function onDispatch(MvcEvent $event) {
// check if content is restricted to the current user
if (content-is-restricted) {
$event->getRouteMatch()->setParam("controller", "{namespace}\Controller\{anotherController}");
$event->getRouteMatch()->setParam("action", "{defaultAction}");
$event->getRouteMatch()->setMatchedRouteName("{defaultRoute}");
}
else {
proceed-as-normal-to-the-action-prescribed-by-route
}
}
不幸的是,上面的代码片段呈现以下错误:
A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
{namespace}\Controller\{anotherController}
No Exception available
如果该操作在同一个 controller 内,则此操作有效,但如果它在不同的操作内则无效。 我错过了什么? 请注意,我不想重定向用户并更改 URL - 只需替换显示给他们的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.