已弃用:在功能系统中检索服务定位器 - ZF2

2023-05-30php开发问题
3

本文介绍了已弃用:在功能系统中检索服务定位器 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17