开发者

Zend Framework - How to call controller's redirect() from within an action helper

开发者 https://www.devze.com 2023-02-21 16:29 出处:网络
I am doing an ACL check in the preDispatch method of an 开发者_如何转开发Action Helper.When it fails, I want to call the action controller\'s _redirect method however I am having difficulty doing this

I am doing an ACL check in the preDispatch method of an 开发者_如何转开发Action Helper. When it fails, I want to call the action controller's _redirect method however I am having difficulty doing this.

In the comments attached to this post, zend-framework, call an action helper from within another action helper, I see two solutions. In the first, the controller is accessed from the helper as $this->_actionController. In the second, it is accessed using $this->getActionController().

I tried both of the following:

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

In either case I get 'Method "_redirect" does not exist ...'. Are there perhaps restrictions on which controller methods can be accessed from the action helper?


There is Redirector action helper that you could use in your action helpers. For example:

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's _redirect method is just a wrapper to the Redirector's gotoUrl method.


example how to do it in preDispatch():

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


Why not use :

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

Or :

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

精彩评论

暂无评论...
验证码 换一张
取 消