简体   繁体   English

从zf2迁移到zf3

[英]Migration of zf2 to zf3

I have some issues regarding the zf2 to zf3 migration of my application. 关于我的应用程序从zf2到zf3的迁移,我有一些问题。 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. 根据迁移指南,控制器中不再有serviceLocator可用。 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. 我以前在每个控制器动作中都使用它,将配置变量数组(位于module.config.php中), Doctrine MongoDB DocumentManagerDoctrine EntityManager注入到需要它们的模型中。 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; 不推荐使用PHP:从v3.0.0版本开始不推荐使用Zend \\ ServiceManager \\ ServiceManager :: getServiceLocator; please use the container passed to the factory instead in /var/www/html/LeapX/vendor/zendframework/zend-servicemanager/src/ServiceManager.php on line 169 请改用第169行的/var/www/html/LeapX/vendor/zendframework/zend-servicemanager/src/ServiceManager.php中传递给工厂的容器

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. 由于我需要访问配置变量并将Doctrine DocumentManagerDoctrine EntityManager注入模型中,因此必须在控制器内调用$this->getServiceLocator() Let me know how to fix this issue. 让我知道如何解决此问题。 Is there any possibility to directly inject these dependencies into my Model classes? 是否可以将这些依赖项直接注入到我的Model类中? 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. 让我知道执行此操作的最佳方法。

From what i have read myself, it is no longer possible to call getServiceLocator() from controllers, they removed it since it promotes antipattern. 从我自己了解的内容来看,不再可能从控制器调用getServiceLocator(),因为它促进了反模式,所以他们将其删除了。

Here's a blog post from Matthew himself on this issue: https://mwop.net/blog/2016-04-26-on-locators.html 这是马修(Matthew)自己关于此问题的博客文章: 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 这个家伙自己的另一个解释(更简短,可能更容易理解): 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. 我目前也在尝试将ZF2(2.4.10)项目迁移到ZF3并面临相同的问题。

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. 我个人同意建议明确定义类和控制器的依赖关系,而不是(过度)使用getServiceLocator()方法,尽管取决于您的代码,为此目的重构大量代码似乎是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 或者,这可能会有所帮助(不过,由于我自己还没有尝试过,所以我不能肯定地说): http : //circlical.com/blog/2016/3/9/preparing-for-zend-f

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM