开发者

Zend_Controller_Action _forward use (or abuse) cases

开发者 https://www.devze.com 2023-01-26 02:17 出处:网络
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 ins

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 request开发者_如何转开发ed 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?


_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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消