Zend Framework: What are the differences between init() and preDispatch() functions in controller objects?

China☆狼群 提交于 2019-12-18 10:09:21

问题


I think the order of execution is init(), preDispatch() and then action() is called.

  1. 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()?

  2. What happens between init() and preDispatch() function calls?


回答1:


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.




回答2:


  • 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.


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




回答3:


init(): Loaded before functions, So if you want to load it before all functions of project, Put it at bootstrap Class. If before a specified class functions, Put it at init() this class function.

preDispatch(): Loaded before the front Controller.



来源:https://stackoverflow.com/questions/2182759/zend-framework-what-are-the-differences-between-init-and-predispatch-functi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!