gpt4 book ai didi

zend-framework - PHP 已弃用 : You are retrieving the service locator from within the class ZFTool\Controller\ModuleController

转载 作者:行者123 更新时间:2023-12-04 12:05:51 27 4
gpt4 key购买 nike

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

$ composer require zendframework/zftool:dev-master 

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

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



我正在使用 Ubuntu

最佳答案

有几个解决方案:

  • 在您的错误报告中,禁用 E_USER_DEPRECATED报告。这
    只是掩盖了问题。
  • 固定到 zend-mvc 的早期版本(例如,
    Composer 需要 "zendframework/zend-mvc:~2.6.0"将固定
    专门针对2.6系列的,不会安装2.7系列的)。
    同样,这只是掩盖了问题,并且可能会离开您的
    如果将安全补丁应用于以后的次要版本,则应用程序不安全
    zend-mvc 的发布。
  • 修复您的代码以不再使用getServiceLocator() .这是推荐的路径。的方式
    完成后一点是确保所有依赖项
    您的 Controller 在实例化期间被注入(inject)。

  • 这将意味着:
  • 您需要为您的 Controller 创建工厂。
  • 您将需要更新您的 Controller 以接受之前从 getServiceLocator() 提取的构造函数中的依赖项。 .
    例如,假设您的 Controller 中有这样的内容:
  • $db = $this->getServiceLocator()->get('Db\ApplicationAdapter');
    您将按如下方式更改您的代码:
  • 添加 $db属性(property)给你的类(class)。
  • 更新您的构造函数以接受数据库适配器,并将其分配给属性。
  • 将上面的行更改为简单的 $db = $this->db (或只使用该属性!)
  • 如果当前不存在,请为您的 Controller 添加一个工厂。

  • 所以:
    use Zend\Db\Adapter\AdapterInterface;
    use Zend\Mvc\Controller\AbstractActionController;

    class YourController extends AbstractActionController
    {
    private $db;

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

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

    你的工厂看起来像这样:
    class YourControllerFactory
    {
    public function __invoke($container)
    {
    return new YourController($this->get('Db\ApplicationAdapter'));
    }
    }

    在您的应用程序或模块配置中,您可以将此工厂映射到您的 Controller :
    return [
    'controllers' => [
    'factories' => [
    YourController::class => YourControllerFactory::class,
    /* ... */
    ],
    /* ... */
    ],
    /* ... */
    ];
    ];

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

    关于zend-framework - PHP 已弃用 : You are retrieving the service locator from within the class ZFTool\Controller\ModuleController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933113/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com