Zend Framework - How to call controller redirection () from action helper

I am doing an ACL check in the preDispatch method of the action helper. When it fails, I want to call the action controller's _redirect method, but it's hard for me to do this.

In the comments attached to this post, zend-framework, call the action assistant from another assistant , I see two solutions. First, the controller gets access 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('/'); 

Anyway, I get the "Method" _redirect "does not exist ...". Are there possibly restrictions on access to controller methods from the action assistant?

+6
php zend-framework
source share
3 answers

There is a Redirector action assistant that you can use in your action assistants. For instance:

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

The controller _ redirect method is just a wrapper for the Redirector gotoUrl method.

+14
source share

An example of how to do this in preDispatch ():

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

Why not use:

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

Or:

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

All Articles