【问题标题】:Zend Framework - How to call controller's redirect() from within an action helperZend 框架 - 如何从动作助手中调用控制器的重定向()
【发布时间】:2011-04-04 02:46:18
【问题描述】:

我正在 Action Helper 的 preDispatch 方法中进行 ACL 检查。当它失败时,我想调用动作控制器的 _redirect 方法但是我很难做到这一点。

在本文所附的 cmets zend-framework, call an action helper from within another action helper 中,我看到了两种解决方案。首先,控制器通过 $this->_actionController 的帮助程序访问。在第二种情况下,使用 $this->getActionController() 访问它。

我尝试了以下两种方法:

$this->_actionController->_redirect('/');
$this->getActionController()->_redirect('/');

在任何一种情况下,我都会得到'方法“_redirect”不存在......'。从动作助手访问哪些控制器方法可能存在限制?

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    您可以在操作助手中使用Redirector action helper。例如:

    class My_Controller_Action_Helper_Test extends Zend_Controller_Action_Helper_Abstract {
    
         public function preDispatch()  {
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');         
            $redirector->gotoUrl('/url');
        }            
    }
    

    Controller 的 _redirect method 只是 Redirector 的 gotoUrl 方法的包装器。

    【讨论】:

      【解决方案2】:

      示例如何在 preDispatch() 中执行:

      $request = $this->getActionController()->getRequest();
      $urlOptions = array('controller' => 'auth', 
                          'action' => 'login');
      $redirector = new Zend_Controller_Action_Helper_Redirector();
      $redirector->gotoRouteAndExit($urlOptions, null, true);
      

      【讨论】:

        【解决方案3】:

        为什么不使用:

        $this->_response->setRedirect('/login');
        $this->_response->sendResponse();
        

        或者:

        $this->_request->setModuleName('default');
        $this->_request->setControllerName('error');
        $this->_request->setActionName('404');
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多