Defense
Defense

Reputation: 259

Zend Framework: Redirect from action helper

That is my Controller Helper:

class Application_Controller_Helper_Test extends Zend_Controller_Action_Helper_Abstract
{
    public function preDispatch()
    {
        $this->_helper->redirector->gotoUrl('/index/index');
        // ...
    }

But have error, which I can't fix:

Call to a member function gotoUrl() on a non-object

Upvotes: 0

Views: 1524

Answers (2)

Zuber Surya
Zuber Surya

Reputation: 869

$this->_redirector = $this->_helper->getHelper('Redirector');

$this->redirector('targetAction', 'targetController');

Upvotes: 0

Florent
Florent

Reputation: 12420

If you want to redirect from an action helper, you have to retrieve the redirector helper from the helper broker. The following snippet will redirect to index/index.

$controller = $request->getControllerName();
$action = $request->getActionName();

// Prevent redirection loop
if ($controller.'/'.$action !== 'index/index') {
    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
    $redirector->gotoSimpleAndExit('index', 'index', 'default');
}

Upvotes: 4

Related Questions