PHP 已弃用:您正在从 ZFToolControllerModuleController 类中检索服务定位器 [英] PHP Deprecated: You are retrieving the service locator from within the class ZFToolControllerModuleController

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

问题描述

我已经使用 composer 安装了 zend 工具

I have installed zend tools using composer

$ composer require zendframework/zftool:dev-master 

zftool 已安装,当我运行 php/vender/bin/zf.php modules list 时,它会抛出警告

zftool has been installed and when I run php /vender/bin/zf.php modules list it's throwing warning

PHP 已弃用:您正在从 ZFToolControllerModuleController 类中检索服务定位器.请注意,ServiceLocatorAwareInterface 已弃用,将在 3.0 版中与 ServiceLocatorAwareInitializer 一起删除....

PHP Deprecated: You are retrieving the service locator from within the class ZFToolControllerModuleController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. ...

我正在使用 Ubuntu

I am using Ubuntu

推荐答案

有几个解决方案:

  • 在您的 error_reporting 中,禁用 E_USER_DEPRECATED 报告.这只是掩盖了问题.
  • 固定到早期版本的 zend-mvc(例如,composer require "zendframework/zend-mvc:~2.6.0" 将固定专门针对 2.6 系列,不会安装 2.7 系列).同样,这只是掩盖了问题,并且可能会使您的如果安全补丁应用于后来的未成年人,则应用程序不安全发布 Zend-mvc.
  • 修复您的代码以使其不再使用getServiceLocator().这是推荐的路径.的方式完成后一点是确保所有依赖项您的控制器是在实例化过程中注入的.
  • In your error_reporting, disable E_USER_DEPRECATED reporting. This just masks the problem.
  • Pin to an earlier version of zend-mvc (e.g., composer require "zendframework/zend-mvc:~2.6.0" will pin specifically to the 2.6 series, and will not install the 2.7 series). This, again, just masks the problem, and will potentially leave your application insecure if security patches are applied to a later minor release of zend-mvc.
  • Fix your code to no longer use getServiceLocator(). This is the recommended path. The way to accomplish this latter point is to ensure that all dependencies for your controller are injected during instantiation.

这意味着:

  • 您需要为控制器创建工厂.
  • 您需要更新控制器以在其构造函数中接受之前从 getServiceLocator() 中提取的依赖项.例如,假设您的控制器中有这样的内容:
  • You need to create factories for your controllers.
  • You will need to update your controllers to accept dependencies in their constructors that were previously pulled from getServiceLocator(). As an example, let's say you had something like this in your controller:

$db = $this->getServiceLocator()->get('DbApplicationAdapter');

您可以按如下方式更改代码:

You would change your code as follows:

  • 为您的类添加一个 $db 属性.
  • 更新您的构造函数以接受数据库适配器,并将其分配给属性.
  • 将上面的行更改为简单的 $db = $this->db(或仅使用该属性!)
  • 为您的控制器添加一个工厂(如果当前不存在).
  • Add a $db property to your class.
  • Update your constructor to accept the database adapter, and assign it to the property.
  • Change the above line to simply $db = $this->db (or just use the property!)
  • Add a factory for your controller, if one does not currently exist.

所以:

use ZendDbAdapterAdapterInterface;
use ZendMvcControllerAbstractActionController;

class YourController extends AbstractActionController
{
    private $db;

    public function __construct(AdapterInterface $db)
    {
        $this->db = $db;
    }

    public function someAction()
    {
        $results = $this->db->query(/* ... */);
        /* ... */
    }
}

您的工厂看起来像这样:

Your factory would look something like this:

class YourControllerFactory
{
    public function __invoke($container)
    {
        return new YourController($this->get('DbApplicationAdapter'));
    }
}

在您的应用程序或模块配置中,您可以将此工厂映射到您的控制器:

In your application or module configuration, you would map this factory to your controller:

return [
    'controllers' => [
        'factories' => [
            YourController::class => YourControllerFactory::class,
        /* ... */
        ],
        /* ... */
    ],
    /* ... */
];
];

这看起来像是很多步骤.但是,它可以确保您的代码没有隐藏的依赖项,提高代码的可测试性,并允许您通过配置做一些很酷的事情,例如替代方案.

This may seem like a lot of steps. However, it ensures your code has no hidden dependencies, improves the testability of your code, and allows you to do cool things like substitute alternatives via configuration.

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

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