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

Issue Type: Docs: Improvement Created: 2011-07-18T02:49:33.000+0000 Last Updated: 2011-07-28T20:05:50.000+0000 Status: Resolved Fix version(s): Reporter: Zachary Burnham (zburnham) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Controller

  • Zend_Controller_Action

Related issues: Attachments:


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) {

public function indexAction()
    // action body

public function forwardAction()



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


Posted by Matthew Weier O'Phinney (matthew) on 2011-07-18T13:40:30.000+0000

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:

<pre class="highlight">
if ('forward' != $this->getRequest()->getActionName()) {
    return $this->_forward('forward');

Posted by Zachary Burnham (zburnham) on 2011-07-18T14:07:23.000+0000

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()."

Posted by Matthew Weier O'Phinney (matthew) on 2011-07-28T20:05:50.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.