Migration of zf2 to zf3

时间:2017-01-03 10:22:10

标签: php zend-framework zend-framework2 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.

1 个答案:

答案 0 :(得分:2)

根据我自己读过的内容,不再可能从控制器调用getServiceLocator(),因为它会提升反模式,所以它们已经删除了它。

以下是马修本人在此问题上发表的博客文章: https://mwop.net/blog/2016-04-26-on-locators.html

这个家伙自己的另一个解释(更短,可能更容易消化): https://github.com/zendframework/zend-mvc/issues/89

我目前正在尝试将ZF2(2.4.10)项目迁移到ZF3,并面临同样的问题。

我个人同意明确定义类和控制器的依赖关系而不是(使用)getServiceLocator()方法的建议,尽管根据您的代码,为此目的重构大量代码似乎是PITA。 / p>

或者,这可能有所帮助(但是,我不能肯定地说,因为我自己没有尝试过): http://circlical.com/blog/2016/3/9/preparing-for-zend-f