简体   繁体   English

如何从另一个 controller 更改操作和调用操作/方法?

[英]How to change action and call action/method from another controller?

I'm using Laminas framework (laminas/mvc v3.1).我正在使用 Laminas 框架(laminas/mvc v3.1)。 In certain cases I need to call a different action than prescribed for a given route, ie display a default page to users who are not authorised to view restricted content.在某些情况下,我需要针对给定路线调用与规定不同的操作,即向无权查看受限内容的用户显示默认页面。

To do this, I've tweaked the onDispatch method in Module.php as follows:为此,我调整了 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 
    }
}

Unfortunately, the snippet above renders the following error:不幸的是,上面的代码片段呈现以下错误:

A 404 error occurred
Page not found.
The requested controller was unable to dispatch the request.
Controller:
{namespace}\Controller\{anotherController}
No Exception available

This works though if the action is within the same controller but not if it's in a different one.如果该操作在同一个 controller 内,则此操作有效,但如果它在不同的操作内则无效。 What am I missing?我错过了什么? Note, I don't want to redirect the user and change the URL - just substitute what is showed to them请注意,我不想重定向用户并更改 URL - 只需替换显示给他们的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从ZF中的任何动作中调用多个控制器动作? - how to call multiple controller action from within any action in ZF? ZF3:如何从控制器中的另一个动作获取一个动作的视图? - ZF3: How to get view of an action from another action in the controller? 在Zend中调用另一个控制器动作并保持当前动作 - Call another controller action in Zend and stay in current action 如何在zend中的同一控制器中调用另一个动作(禁用了ajax布局)中的一个动作 - how to call one action in another action(whose ajax layout is disabled) in same controller in zend Zend Framework:如何从视图脚本中调用控制器动作助手? - Zend Framework: How to call a controller action helper from a view script? 如何从Zend框架中的javascript调用控制器和动作 - How do call the controller and action from javascript in Zend framework ZEND控制器 - 如何从不同的控制器调用操作 - ZEND Controllers — How to call an action from a different controller 如何从独立的php文件调用zend控制器动作? - How to call a zend controller action from an independent php file? 如何从其他控制器调用动作-ZendFramework1 - How to call an action from an other controller - ZendFramework1 Zend框架 - 从当前操作向Controller中的另一个操作发送消息 - Zend framework - send a message from the current action to another action in Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM