Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 568
Developer Avatar asked Aug 13 '12 13:08

Developer


2 Answers

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

like image 133
superdweebie Avatar answered Sep 28 '22 07:09

superdweebie


I think you can drop this into a controller and it will work.

public function onDispatch(MvcEvent $e)
like image 42
Tom Shaw Avatar answered Sep 28 '22 06:09

Tom Shaw