Migration of zf2 to zf3


I have some issues regarding the zf2 to zf3 migration of my application. I've gone through the migration guides and started the migration process as describe there.

According to the migration guide, there is no serviceLocator available into controllers anymore. And I used to use it within each and every controller action to inject the config variable array (located in module.config.php), Doctrine MongoDB DocumentManager and the Doctrine EntityManager into the Models where they are needed. Now I'm getting so many deprecated warnings messages as below;

PHP Deprecated: Usage of Zend\ServiceManager\ServiceManager::getServiceLocator is deprecated since v3.0.0; please use the container passed to the factory instead in /var/www/html/LeapX/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 169

Since I need to access config variables and inject the Doctrine DocumentManager and Doctrine EntityManager into my Models, I had to call $this->getServiceLocator() within my controllers. Let me know how to fix this issue. Is there any possibility to directly inject these dependencies into my Model classes? Should I need to use factories for Models?

And the other question is when it comes to factories regarding the controllers, Should I need to create individual factory for each and every controller of my application? There are quite a lot of number of controllers spread within few Modules in the application. If I add dedicated factory for each and every controller there will be double the number. Let me know the best way to do this.

Show source
| php   | zend-framework   | zend-framework2   | zf3   2017-01-03 11:01 1 Answers

Answers to Migration of zf2 to zf3 ( 1 )

  1. 2017-01-03 13:01

    From what i have read myself, it is no longer possible to call getServiceLocator() from controllers, they removed it since it promotes antipattern.

    Here's a blog post from Matthew himself on this issue: https://mwop.net/blog/2016-04-26-on-locators.html

    Another explanation from the guy himself here (shorter, might be easier to digest): https://github.com/zendframework/zend-mvc/issues/89

    I am currently also in the process of trying to migrate a ZF2 (2.4.10) project to ZF3 and face the same issue.

    I personally agree with the recommendation to explicitly define dependencies of your classes and controllers instead of (over)using getServiceLocator() method, though depending on your code, refactoring lots of code for this purpose might seem to be a PITA.

    Alternatively, this can probably help (though, i can't say for sure since i haven't tried this myself): http://circlical.com/blog/2016/3/9/preparing-for-zend-f

Leave a reply to - Migration of zf2 to zf3

◀ Go back