6

在 Zendframework 1 中,我们使用init()方法来初始化控制器中的东西。我看到这是从zenframework 2中取出来的。为什么?以及在 zf 2 中实现相同目标的最佳方法是什么。我正在升级我以前在 zf1 中开发的项目,我可以看到 zf2 与 zf1 相比发生了很大变化。

zf2 中是否还有其他变化,我们使用其他方法(例如zf1 中的preDispatch()postDispatch())的方式?

有人经历过吗?

4

3 回答 3

11

In zf2 controllers are instantated by the ControllerLoader, which is a subclass of the ServiceManager. If you need to initalize a controller, either use a Factory, or __construct. Use __construct for simpile initalizations, and use a Factory if the controller consumes other objects that need to be injected.

preDispatch and postDispatch are also gone in favour of the new events system. To get the same result in zf2, register event handlers for the disptach and render events. For a full list of mvc envents see http://akrabat.com/zend-framework-2/a-list-of-zf2-events/

Also, take a look here for an example of setting up a controller factory ZF2 how to get entity Manager from outside of controller

于 2012-08-13T22:22:42.400 回答
8

我认为您可以将其放入控制器中,它会起作用。

public function onDispatch(MvcEvent $e)
于 2013-04-22T19:35:46.717 回答
0

由于 OP 提到postDispatch,值得注意的是__destruct现在以类似的方式工作。然而,一个很大的区别是不能阻止执行(例如通过exit;)来破坏对象。

于 2015-01-11T19:32:32.867 回答