2011-04-04 83 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'); 
相關問題