4

While developing a web app using ZF, I had an haha! moment regarding the _forward method in Zend_Controller_Action. As stated in the Programmer's Reference Guide, when calling the _forward method inside an action, the requested action will not be executed until the current action completes. This begs the question:

When would you use the _forward action to intentionally make sure your current action completes before starting another, aside from form processing (although you would probably place the _forward request at the end of the action anyway)? What are some clear cut examples of this? Any pitfalls or advantages to using this approach as apposed to an ActionStack?

Fatmuemoo
  • 2,187
  • 3
  • 17
  • 34

3 Answers3

4

_forward() just replaces module/controller/action parameters in Request object.

It just allows to change your mind on the go (without another request).

This has different consequences, depending on which dispatch loop state it is called. Some time setDispatched() is needed to execute.

Consider those scenarios:

First:

$this->_forward('some')

Second:

return $this->_forward('some');

Third:

$this->someAction();
// ececuted?

Fourth:

return $this->someAction();
// executed?
takeshin
  • 49,108
  • 32
  • 120
  • 164
  • +1 for the 2 examples. It never occurred to me that _forward('some') == $this->someAction(). However, I'm really interested in knowing when to use it if I'm not using it as a exit point and the advantages/disadvantage over the ActionShack helper. – Fatmuemoo Nov 17 '10 at 20:03
  • 1
    The main disadvantage of `ActionStack` is multiple calling the dispatch loop. You will avoid this for sure using `return` when forwarding to different controllers or use `$this->someAction()` when in the same controller. My advice: *always return*. It's good for unit testing too. – takeshin Nov 17 '10 at 21:51
1

I really only use _forward for two reasons:

  1. I want to redirect but don't want the user's URL to change.

  2. I want to pass some (non-string) object to another action.

    $this->_forward('index', null, null, array('create_task_form' => $form));

In each case, the target action can stand by itself, without the originator, and usually just marshals up the display.

Derek Illchuk
  • 5,638
  • 1
  • 29
  • 29
0

When would you use the _forward action to intentionally make sure your current action completes before starting another

I don't think it's used to let the current action complete before _forward() is used. That looks more like a call to your domain logic (model, service, something like that) than handling a request which is what an action is for.

yourAction
  if(conditionsAreNotMet()) {
     return _forward(anotherAction);
  }

I think _forward() is provided to have an early exit point in your action (which logic is all focused on one thing, for example presenting news); without the need of performing another request (like _redirect()) and thus putting more load on the web server.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Aha! But its not an exit point at all. If you call _forward() in your action, it will not forward until the current action completes. You can return _forward to exit, but just calling _forward doesn't exit the current action. – Fatmuemoo Nov 17 '10 at 16:15
  • @Fatmuemoo: I agree with your points but the early exit point is the only reason I've ever used it, because: if I know where to `_forward()` anything, then I probably have all the prerequisites to handle the request in the same action. I'd rather have some duplicate calls to models/services, than order a `_forward()` to an action that I know **presently** means a logic continuation of my first action, but might very well change since the other action never should have any dependencies. Maybe I've just not seen a real use case yet, have you got any? :) – chelmertz Nov 17 '10 at 22:21