已弃用:在功能系统中检索服务定位器 - ZF2 [英] Deprecated: Retrieve service locator in functional system - ZF2

查看:17
本文介绍了已弃用:在功能系统中检索服务定位器 - ZF2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 ZF2 系统,它运行良好,但是在我在其他计算机上克隆存储库后,出现了这个已弃用的错误:

I'm developing a ZF2 system and it was working very well, but after I clone the repository in other computer this deprecated error has appeared:

您正在从 ModuleControllerController 类中检索服务定位器.请注意,ServiceLocatorAwareInterface 已弃用,将在 3.0 版中与 ServiceLocatorAwareInitializer 一起删除.您需要更新您的类以在创建时接受所有依赖项,通过构造函数参数或设置器,并使用工厂来执行注入.在第 258 行的/home/path/project/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php 中

You are retrieving the service locator from within the class ModuleControllerController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. You will need to update your class to accept all dependencies at creation, either via constructor arguments or setters, and use a factory to perform the injections. in /home/path/project/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php on line 258

composer.json:

The composer.json:

"require": {
    "php": ">=5.5",
    "ext-curl": "*",
    "ext-json": "*",
    "ext-mbstring": "*",
    "zendframework/zendframework": "~2.5",
    "doctrine/doctrine-orm-module": "0.*",
    "hounddog/doctrine-data-fixture-module": "0.0.*",
    "imagine/Imagine": "~0.5.0"

在我的控制器中检索服务时出现错误(扩展 ZendMvcControllerAbstractActionController):

The error appears when I retrieve the service in my controllers (extending ZendMvcControllerAbstractActionController):

$this->getServiceLocator()->get("ModuleServiceService");

在 Zend 内核中 ZendMvcControllerAbstractController 是这样的:

In the Zend core at ZendMvcControllerAbstractController is like this:

public function getServiceLocator()
{
    trigger_error(sprintf(
        'You are retrieving the service locator from within the class %s. Please be aware that '
        . 'ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along '
        . 'with the ServiceLocatorAwareInitializer. You will need to update your class to accept '
        . 'all dependencies at creation, either via constructor arguments or setters, and use '
        . 'a factory to perform the injections.',
        get_class($this)
    ), E_USER_DEPRECATED);

    return $this->serviceLocator;
}

以前只有这个:

public function getServiceLocator()
{
    return $this->serviceLocator;
}

我什么都试过了,有人知道我该怎么做吗?

I've tried everything, someone know what I've to do?

推荐答案

您无需执行任何操作,还没有.当您升级到 ZF3 时,您将不得不更改控制器类接收其依赖项的方式.

You don't have to do anything, yet. When you upgrade to ZF3, then you will have to change how your controller class receives its dependencies.

ZF2 支持两种依赖注入模式:通过服务定位器和通过构造函数.ZF3 删除了按服务位置"并需要按构造函数".所有这一切都有效地改变了依赖关系的解决方式,将解决方案从及时"转移到构建时".

ZF2 supports two dependency injection patterns: by service locator and by constructor. ZF3 removes "by service location" and requires "by constructor". All this does, effectively, is change how dependencies resolve, moving the resolution from "just in time" to "at construction".

您不能从任何地方获得服务,而是在施工处接收它们.按照以下几行更新您的代码:

Instead of being able to get a service from anywhere, you instead receive them at construction. Update your code along the following lines:

namespace ModuleController;

class Controller {
    public function __construct(ModuleServiceService $service) {
        $this->service = $service;
    }
}

在类的方法中需要$this->service 的地方使用它.

Use $this->service where you need it in the class's methods.

然后使用控制器工厂来创建您的控制器,如下所示:

Then use a controller factory to create your controller, like so:

function ($controllers) { 
    $services = $controllers->getServiceLocator();
    return new ModuleControllerController($services->get('ModuleServiceService')); 
} 

问题 5168这篇博文 讨论了为什么使用服务定位器的服务注入是一种反模式.

The change is discussed in Issue 5168, and this blog post discusses why service injection with a service locator is an anti-pattern.

这篇关于已弃用:在功能系统中检索服务定位器 - ZF2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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