名为“ getServiceLocator”的插件;在插件管理器Zend\Mvc\Controller\PluginManager中找不到 [英] A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager

查看:143
本文介绍了名为“ getServiceLocator”的插件;在插件管理器Zend\Mvc\Controller\PluginManager中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近开始通过一门课程学习AngularJS和Zend Framework 2。鉴于本课程的年份,如果我记得是2013年,则这两个框架中的某些情况都已发生变化。很快,我遇到了一个问题,使用以下代码片段测试了与数据库的连接并使用Doctrine 2列出了记录:

Recently started learning AngularJS and Zend Framework 2 through a course. Given the year of the course, which if I remember is 2013, some things have changed in both frameworks. Soon, I came across a problem using the following code snippet to test the connection to the database and list the records using Doctrine 2:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController {
    public function indexAction() {
        $em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        $repo = $em->getRepository('Entity\Categoria');

        $categorias = $repo->findAll();

        return new ViewModel(['categories'=>$categorias]);
    }
}

当我运行时,它返回以下错误:

When I run, it returns the following error::


在插件管理器Zend\Mvc\Controller\PluginManager中找不到名为 getServiceLocator的插件>

A plugin by the name "getServiceLocator" was not found in the plugin manager Zend\Mvc\Controller\PluginManager

此外,附加信息:


Zend\ \ServiceManager\Exception\ServiceNotFoundException

Zend\ServiceManager\Exception\ServiceNotFoundException

文件:

C:\xampp\htdocs\Curso de ZF2\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php:133

据我所知,问题源于 getServiceLocator()已从最新版本的Zend Framework 2中删除。但是,我不知道如何解决此问题,以便继续测试。有人可以帮助我吗?

As far as I know, the problem stems from the fact that the getServiceLocator() had been removed from the latest version of Zend Framework 2. However, I have no idea how to solve this so that I can continue testing . Someone can help me?

推荐答案

如果您已经从作曲家那里更新或签出过Zend Framework 3,并且仅仅是为了使您的作曲家当然,您可以将其降级到早期版本(2.x),其中 getServiceLocator()可用,但已弃用。从3.0开始将其删除。

If you have updated or checked out Zend Framework 3 from composer, and if it's just for making your course material work, you could downgrade to an earlier (2.x) version, where getServiceLocator() is available, yet deprecated. It was removed from 3.0 onwards.

更好的方法是了解如何解决它,因为无论如何将来您都必须这样做。基本上,您不应该在运行时中间注入依赖项,而应该为您的控制器实际注册一个工厂,然后通过构造函数将依赖项传入。可能的修复方法在以下问题的可接受答案中得到了很好的解释:

A better approach would be to understand how to work around it, since you will have to do it in the future, anyway. Basically, you shouldn't inject dependencies in the middle of runtime, but actually register a factory for your controller and then pass dependencies in through a constructor. Possible fixes are explained well in the accepted answer for the following question:

PHP Deprecated: You are retrieving the service locator from within the class ZFTool\Controller\ModuleController

在上面的示例中, $ db = $ this-> getServiceLocator()-> get('Db\ApplicationAdapter'); 作为构造函数参数传递,因此它将立即可用于Controller。因此,您应该以类似的方式为 IndexController 创建一个Factory,然后使用 Doctrine\ORM\EntityManager 已经通过构造函数注入了(切记用您的真实模块名称替换 yourModule):

In the example above, $db = $this->getServiceLocator()->get('Db\ApplicationAdapter'); is passed as a constructor argument, so it will be immediately available to the Controller. So in a similar fashion, you should create a Factory for your IndexController, which should return it with Doctrine\ORM\EntityManager already injected through a constructor (remember to replace "yourModule" with your real module name):

namespace yourModule\Controller\Factory;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use yourModule\Controller\IndexController;

class IndexControllerFactory implements FactoryInterface {
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
        return new IndexController($container->get('Doctrine\ORM\EntityManager'));
    }
}

您可以将Factory类放置在任何地方,只要一切都进行了相应配置。在此示例中,我将其放入 /Module/yourModule/src/yourModule/Controller/Factory/IndexControllerFactory.php

You could place your Factory class anywhere, as long as everything is configured accordingly. In this example, I've put it in /Module/yourModule/src/yourModule/Controller/Factory/IndexControllerFactory.php.

调用上面的类后,您的 $ em 变量将被填充,并可以在控制器中的任何位置调用(注意,新属性 $ em 及其用法: $ this-> em ):

With the class above being called, your $em variable would then be populated and could be called from anywhere in the controller (note the new property $em and its usage: $this->em):

class IndexController extends AbstractActionController {

    protected $em;

    public function __construct(EntityManager $em) {
        $this->em = $em;
    }

    public function indexAction() {
        $repo = $this->em->getRepository('Entity\Categoria');
        $categorias = $repo->findAll();
        return new ViewModel(['categories'=>$categorias]);
    }

    ...

}

现在,在yourModule的 module.config.php 中注册新工厂,我们就完成了:

Now, register you new factory in yourModule's module.config.php and we're done:

<?php
return [
    // ... other stuff
    // ...
    'controllers' => [
        'factories' => [  
            'yourModule\Controller\Index' => 'yourModule\Controller\Factory\IndexControllerFactory',
            ],
    ],
    // etc...
]; 
// end of file

非常重要:可能具有相似的内容,但是使用调用作为数组键。请注意,您将使用工厂作为键名(因为您正在声明一个控制器工厂),而不是 invokes,这是针对没有依赖项或常规控制器的类。

Very important: your file will probably have similar contents, but using "invokes" as array key. Notice that you will use factories as key name (because you are declaring a controller factory), instead of "invokes", which is for classes without dependencies or regular controllers.

我还建议研究此 Zend Framework 3迁移指南,它提供了很多重要的信息,可能会有所帮助。

I also recommend studying this Migration guide for Zend Framework 3, it has a lot of important information that might help.

关于该课程,我再次建议您将PHP降级为兼容版本(如果您使用的是作曲家,则非常容易),或者尝试在网上找到其他最新的课程或教程。 ZF2在版本之间发生了重大变化,这不是您会发现的唯一错误,而且您会感到困惑而不是学习。让我知道这是否适合您。

As for that course you are following, again, I suggest you downgrade PHP to a compatible version (very easy if you're using composer), or try to find another up-to-date course or tutorial on the web. ZF2 changed significantly between versions and chances are this is not the only error you will find and you'll get very confused instead of learning. Let me know if this works for you.

这篇关于名为“ getServiceLocator”的插件;在插件管理器Zend\Mvc\Controller\PluginManager中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆