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
I think you can drop this into a controller and it will work.
public function onDispatch(MvcEvent $e)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With