ZF-11571: call to ->_forward() from preDispatch method of Zend_Controller_Action subclass causes infinite loop

Description

Adding a call to _forward() in the preDispatch() method of a subclass of Zend_Controller_Action gives an infinite loop.

My controller:

class IndexController extends Zend_Controller_Action {

public function init()
{
    /* Initialize action controller here */
}

public function preDispatch()
{
    if (true) {
        $this->_forward('forward');
    }
}

public function indexAction()
{
    // action body
}

public function forwardAction()
{

}

}

This is after a stock install of ZF using the shell script.

Comments

Calling _forward() in a controller to an action in the same controller is no different than calling one to an action in a different controller. In each case, a new iteration of the dispatch loop is invoked, which means that the preDispatch() method will be called again.

As such, you need to add some logic to test against the current action before forwarding:


if ('forward' != $this->getRequest()->getActionName()) {
    return $this->_forward('forward');
}

It would be good if the documentation mentioned the possibility of a loop, lest someone new to ZF and to MVC in general make the same mistake I did. Something like:

"Note that using _forward() will cause the preDispatch() method to be called again, so to avoid a loop, add logic to check for the action name around your call to _forward()."

Edited to make this a documentation issue. Zach, would you like to tackle it?