我正在Action Helper的preDispatch方法中执行ACL检查。当它失败时,我想调用动作控制器的_redirect方法,但是我很难做到这一点。
在zend-framework, call an action helper from within another action helper这篇文章的评论中,我看到了两种解决方案。在第一个示例中,控制器作为$this->_actionController从帮助器访问。在第二个示例中,可以使用$this->getActionController()访问它。
我尝试了以下两种方法:
$this->_actionController->_redirect('/');
$this->getActionController()->_redirect('/');
无论哪种情况,我都会得到‘_redirect’方法不存在...‘。对于可以从操作帮助器访问的控制器方法,是否存在限制?
发布于 2011-04-04 03:59:53
你可以在动作帮助器中使用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
方法的包装器。
发布于 2011-04-04 02:58:41
示例如何在preDispatch()中实现:
$request = $this->getActionController()->getRequest();
$urlOptions = array('controller' => 'auth',
'action' => 'login');
$redirector = new Zend_Controller_Action_Helper_Redirector();
$redirector->gotoRouteAndExit($urlOptions, null, true);
发布于 2012-03-15 10:55:04
为什么不使用:
$this->_response->setRedirect('/login');
$this->_response->sendResponse();
或者:
$this->_request->setModuleName('default');
$this->_request->setControllerName('error');
$this->_request->setActionName('404');
https://stackoverflow.com/questions/5534111
复制