简体   繁体   中英

init method in zf2 controller

In Zendframework 1 we use init() method for initialize stuff in controller. I saw that this is taken out from zenframework 2. Why? and what is the best way to achieve same thing in zf 2. I am upgrading my previous project developed in zf1 and I can see things has changed a lot in zf2 as compare to zf1.

Is there anyother change in zf2, they way we use other methods such as preDispatch() and postDispatch() in zf1?

Anyone has gone through this?

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

我想你可以把它放到一个控制器中,它会工作。

public function onDispatch(MvcEvent $e)

Since OP mentions postDispatch , it's worth noting that __destruct now works in a similar manner. One big difference, though, is that execution cannot be prevented (eg through exit; ) turing tear-down of the Object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM