我认为执行的顺序是init()、preDispatch()然后action()被调用.
我是否应该在 init() 或 preDispatch() 中初始化我的变量,这些变量在所有操作中都很常见?我见过有人使用这两个函数进行初始化.可能顾名思义,它应该在 init() 中完成,但是在 preDispatch() 中会发生什么样的事情?
init() 和 preDispatch() 函数调用之间会发生什么?
首先为 Zend_Controller_Plugin_Abstract
的实例调用 preDispatch()
.这里有请求和响应对象,因此您可以过滤请求或使用请求中的信息做一些准备.
Zend_Controller_Action
的 init()
接下来作为构造函数的一部分被调用.它可以帮助您初始化控制器,而无需覆盖和重复构造函数的签名(Zend_Controller_Action::__contruct()
).
控制器的preDispatch()
方法在这里被调用.您可以调用 $request->setDispatched(false)
来跳过当前操作 - 不确定您是否可以在 init()
然后调用您的操作方法(例如viewAction()
).在这里,您可以执行正常的工作,例如从模型中获取内容并填充视图.
所以区别现在应该很清楚了:
preDispatch()
之外,还有 routeStartup
和 其他),init
或 preDispatch()
,init()
和 preDispatch()
函数调用之间会发生什么?
几乎什么都没有 - preDispatch()
被执行了,如果你还没有调用 $request->setDispatched(false)
,动作就会被执行.>
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 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:
preDispatch()
there is routeStartup
and others),init
or preDispatch()
,What happens between
init()
andpreDispatch()
function calls?
Almost nothing - preDispatch()
is executed, and if you haven't called $request->setDispatched(false)
, the action is executed.
这篇关于Zend Framework:控制器对象中的 init() 和 preDispatch() 函数有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!