Zend Framework:控制器对象中的init()和preDispatch()函数之间有什么区别? [英] Zend Framework: What are the differences between init() and preDispatch() functions in controller objects?

查看:209
本文介绍了Zend Framework:控制器对象中的init()和preDispatch()函数之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为执行的顺序是init(),preDispatch(),然后调用action().

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

  1. 我应该在init()或preDispatch()中初始化我所有操作中都通用的变量吗?我见过人们同时使用这两个函数进行初始化.顾名思义,它应该在init()中完成,但是preDispatch()中会放入什么样的东西呢?

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

init()和preDispatch()函数调用之间会发生什么?

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

推荐答案

对于Zend_Controller_Plugin_Abstract的实例,首先调用preDispatch().这里有请求和响应对象,因此您可以过滤请求或使用请求中的信息进行一些准备.

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()作为构造函数的一部分.它可以帮助您初始化控制器,而不必覆盖和重复构造函数的签名(Zend_Controller_Action::__contruct()).

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

在此调用控制器的preDispatch()方法.您可以调用$request->setDispatched(false)跳过当前操作-不确定是否可以在init()

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

然后将调用您的操作方法(例如,viewAction()).在这里,您可以进行常规工作,例如从模型中获取内容并填充视图.

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()之外,还有routeStartup
  • 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.

init()preDispatch()函数调用之间会发生什么?

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

几乎什么都没有-将执行preDispatch(),如果未调用$request->setDispatched(false),则将执行该动作.

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

这篇关于Zend Framework:控制器对象中的init()和preDispatch()函数之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆