Thursday, July 14, 2011

zend framework _forward utility method

From the official document:"_forward($action, $controller = null, $module = null, array $params = null): perform another action. If called in preDispatch(), the currently requested action will be skipped in favor of the new one. Otherwise, after the current action is processed, the action requested in _forward() will be executed."

I mention this method here because it seems many people haven't noticed the last sentence: " Otherwise, after the current action is processed, the action requested in _forward() will be executed". And i have seen several times people write codes like this in action methods:

if ($condition) {
                $this->_forward($action);
}
//still do some tasks here
$this->doSomething();

It is easy to assume that once we need to forward a request, the rest of the code won't get executed. Unfortunately, this is not necessarily true. _forward() is different from _redirect(). So it is better that you design your code carefully to ensure that once you need to forward the request, the rest of the code won't get executed.

No comments: