不推荐使用:检索功能系统中的服务定位器-ZF2 [英] Deprecated: Retrieve service locator in functional system - ZF2

查看:94
本文介绍了不推荐使用:检索功能系统中的服务定位器-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:

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

You are retrieving the service locator from within the class Module\Controller\Controller. 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"

当我在控制器(扩展Zend \ Mvc \ Controller \ AbstractActionController)中检索服务时,出现错误:

The error appears when I retrieve the service in my controllers (extending Zend\Mvc\Controller\AbstractActionController):

$this->getServiceLocator()->get("Module\Service\Service");

在Zend \ Mvc \ Controller \ AbstractController的Zend核心中,是这样的:

In the Zend core at Zend\Mvc\Controller\AbstractController 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时,您将不得不更改 controller类接收其依赖项的方式.

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".

您无法从任何地方获取服务,而是在 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 Module\Controller;

class Controller {
    public function __construct(\Module\Service\Service $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 \Module\Controller\Controller($services->get('Module\Service\Service')); 
} 

问题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天全站免登陆