Init method in zf2 controller

In Zendframework 1, we use the init() method to initialize an element in the controller. I saw that it is derived from zenframework 2. Why? and what is the best way to achieve the same in zf 2. I am updating my previous project developed in zf1 and I see that in zf2 a lot has changed compared to zf1.

Is there another change in zf2, do they use other methods like preDispatch() and postDispatch() in zf1?

Has anyone gone through this?

+6
source share
3 answers

In zf2 controllers, a ControllerLoader is created, which is a subclass of ServiceManager. If you need to initialize the controller, use either Factory or __construct . Use __construct to initialize simpile and use Factory if the controller consumes other objects that need to be inserted.

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

We also consider an example of Factory ZF2 controller configuration, how to get the object manager from outside the controller

+12
source

I think you can throw this at the controller and it will work.

 public function onDispatch(MvcEvent $e) 
+8
source

Since the OP mentions postDispatch , it is worth noting that __destruct now works in a similar way. However, the big difference is that execution cannot be prevented (for example, through exit; ) from breaking off the Object.

0
source

Source: https://habr.com/ru/post/922725/


All Articles