I think the order of execution is init(), preDispatch() and then action() is called.
-
Should I initialize my variables, which are common among all actions, in init() or preDispatch()? I’ve seen people using both functions for initialization. Probably as the name suggests it should be done in init() but then what kind of stuff would go in preDispatch()?
-
What happens between init() and preDispatch() function calls?
First
preDispatch()is called for instances ofZend_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 theZend_Controller_Actionis 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 ininit()Then your action method is called (
viewAction()for example). Here you do your normal work like fetching stuff from the model and populating the view.So the distinction should now be clear:
preDispatch()there isrouteStartupand others),initorpreDispatch(),Almost nothing –
preDispatch()is executed, and if you haven’t called$request->setDispatched(false), the action is executed.