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()
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:
- 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.