2011-04-04 86 views
6

我正在执行动作助手的preDispatch方法中的ACL检查。当它失败时,我想调用操作控制器的_redirect方法,但是我很难做到这一点。Zend Framework - 如何从动作助手内部调用controller的重定向()

在附加到这篇文章的评论中,我看到了两个解决方案:zend-framework, call an action helper from within another action helper。在第一个中,控制器是通过$ this - > _ actionController从助手中访问的。在第二个中,它使用$ this-> getActionController()来访问。

我尝试以下两个:

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

在这两种情况下,我得到“方法‘_redirect’不存在......”。是否可以限制哪些控制器方法可以从动作助手访问?

回答

14

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'); 
    }    
} 

控制器的_redirect method只是一个包装器重定向的gotoUrl方法。

0

例如如何做到这一点的preDispatch()方法:

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

为什么不使用:

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

或者:

$this->_request->setModuleName('default'); 
$this->_request->setControllerName('error'); 
$this->_request->setActionName('404'); 
相关问题