Pre- and Post-Dispatch Hooks in zend framework

Zend_Controller_Action specifies two methods that may be called to bookend a requested action, preDispatch() and postDispatch(). These can be useful in a variety of ways: verifying authentication and ACL‘s prior to running an action (by calling _forward() in preDispatch(), the action will be skipped), for instance, or placing generated content in a sitewide template (postDispatch()).
Use of init() vs. preDispatch().

Pre- and Post-Dispatch Hooks in zend framework


The init() method is primarily intended for extending the constructor. Typically, your constructor should simply set object state, and not perform much logic. This might include initializing resources used in the controller (such as models, configuration objects, etc.), or assigning values retrieved from the front controller, bootstrap, or a registry. 

The preDispatch() method can also be used to set object or environmental (e.g., view, action helper, etc.) state, but its primary purpose is to make decisions about whether or not the requested action should be dispatched. If not, you should then _forward() to another action, or throw an exception. 

or

Like method name itself tells you, this method is called before dispatching process takes place, so it is called before the action method. So we could use it to modify headers, or set different sitewide template. We can also modify current request and set different dispatch flag (skip current action processing).  For example: we check if the user  hasIdentity() to access some password protected site. If not, we redirect him back to the login page. So, more logic applied here than in init() method.

First preDispatch() is called for instances of Zend_Controller_Plugin_Abstract. Here you have the request and response objects, so you might filter the request or do some preparation using the information from the request.

init() of the Zend_Controller_Action is called next as part of the constructor. It’s there to help you initialize your controller, without having to override and repeat the signature of the constructor (Zend_Controller_Action::__contruct()).

The controller’s preDispatch() method is called here. You can call $request->setDispatched(false) to skip the current action – not sure if you can do that in init().

If you want something to be executed before all actions - put it in a plugin and use one of the hooks (besides preDispatch() there is routeStartup and others),
    if you want before every action in a controller - init or preDispatch(),
    if only for a single action - the action itself.
What happens between init() and preDispatch() function calls? 

Almost nothing - preDispatch() is executed, and if you haven't called $request->setDispatched(false), the action is executed.

Note: _forward() actually will not work correctly when executed from init(), which is a formalization of the intentions of the two methods.  

No comments:

Post a Comment

Download the eBook of react js for beginners

ReactJS basically is an open-source JavaScript library which is used for building user interfaces specifically for single page applications...

Popular