Doctrine2 更新导致 Zend Framework 3 中的 AnnotationRegistry registerLoader 错误

Doctrine2 Update Caused AnnotationRegistry registerLoader Error in Zend Framework 3

我正在开发基于 Zend Framework 3.0 的 CMS,以使用 Doctrine 管理 DB I。使用 composer 管理包时我的问题是什么?最近,我将所有包更新到最新版本并将其发送到服务器,其他文件没有任何变化。更新后我的网站显示以下错误:

Fatal error: Uncaught TypeError: Return value of Doctrine\Common\Annotations\AnnotationRegistry::registerLoader() must be an instance of Doctrine\Common\Annotations\void, none returned in /home/platne/serwer18346/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php:117 Stack trace: #0 /home/platne/serwer18346/vendor/doctrine/doctrine-module/src/DoctrineModule/Module.php(57): Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(Object(Closure)) #1 /home/platne/serwer18346/vendor/zendframework/zend-modulemanager/src/Listener/InitTrigger.php(33): DoctrineModule\Module->init(Object(Zend\ModuleManager\ModuleManager)) #2 /home/platne/serwer18346/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\ModuleManager\Listener\InitTrigger->__invoke(Object(Zend\ModuleManager\ModuleEvent)) #3 /home/platne/serwer18346/vendor/zendframework/zend-eventmanager/src/EventManager.php(171): Zend\EventManager\EventManager->triggerListeners(Object(Zend\ModuleManager\ModuleEvent)) #4 /home/p in /home/platne/serwer18346/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationRegistry.php on line 117

一些应用程序代码(如果需要):
模块:

return [
    'Zend\Router',
    'Zend\Validator',
    'DoctrineModule',
    'DoctrineORMModule',
    'Core',
];

development.local(开发者模式已激活):

'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => Doctrine\DBAL\Driver\PDOMySql\Driver::class,
                'params' => [
                    'host' => '******',
                    'user' => '*******',
                    'password' => '******',
                    'dbname' => '*******',
                    'charset' => 'utf8'
                ]
            ]
        ]
    ]

module.config:

'doctrine' => [
        'driver' => [
            __NAMESPACE__ . '_driver' => [
                'class' => AnnotationDriver::class,
                'cache' => 'array',
                'paths' => [__DIR__.'/../src/Model']
            ],
            'orm_default' => [
                'drivers' => [
                    __NAMESPACE__ . '\Model' => __NAMESPACE__ . '_driver'
                ]
            ]
        ]
    ]

控制器工厂:

public function __invoke(ContainerInterface $container,$requestedName, array $options = null)
{
    $controllerInstance = null;
    switch($requestedName){
        case 'Core\Controller\IndexController': $controllerInstance = $this->_invokeIndex($container); break;
        case 'Core\Controller\PagesController': $controllerInstance = $this->_invokePages($container); break;
    }
    return $controllerInstance;
}

protected function _invokeIndex(ContainerInterface $container)
{
    return new Controller\IndexController(
        $container->get('doctrine.entitymanager.orm_default')
    );
}

protected function _invokePages(ContainerInterface $container)
{
    return new Controller\PagesController(
        $container->get('doctrine.entitymanager.orm_default')
    );
}

控制器父级:

 protected $_entityManager;

    /**
     * AppController constructor.
     * @param EntityManager $entityManager
     */
    public function __construct(EntityManager $entityManager)
    {
        $this->_entityManager = $entityManager;
    }

    /**
     * @return EntityManager
     */
    public function getEntityManager()
    {
        return $this->_entityManager;
    }

正如我所说,此代码在更新前有效。更新后它告诉我那个错误,更重要的是在上传以前的版本后错误仍然存​​在。我试过改写代码,效果一样。

作曲家(无项目数据):

"require": {
    "zendframework/zend-mvc": "*",
    "zendframework/zend-developer-tools": "*",
    "zendframework/zend-session": "*",
    "zendframework/zend-authentication": "*",
    "zfcampus/zf-development-mode": "*",
    "doctrine/doctrine-orm-module": "*"
  },
  "autoload": {
    "psr-4": {
      "Core\": "module/Core/src/"
    }
  }

此错误是由于最新版本Doctrine\Common\Annotations使用PHP7.1造成的。这就是为什么它使用 void 作为 return type。 PHP 7.0.* 不支持它。这是new feature in PHP 7.1

我在使用 PHP 7.0 的 ZF3 项目中使用 doctrine-orm-module 1.1。它运作良好。所以,只需将 doctrine-orm-module 版本替换为 1.1.

"doctrine/doctrine-orm-module": "^1.1"

我建议您定义您在 composer 中使用的依赖项的版本。这是为了让您的项目在新版本的依赖项发布时不会被破坏。

您可以尝试使用以下配置。它对我有用。

    "require": {
        "php": ">=5.5.9",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "2.5.6",
        "doctrine/annotations": "1.4.*",
        "doctrine/dbal": "2.5.4",
        ...
     }

当您报告 composer/package 问题时,composer show 的输出也非常有用。我的看起来像这样:

doctrine/annotations                 v1.4.0  Docblock Annotations Parser
doctrine/cache                       v1.7.0  Caching library offering an object-oriented API for many cache backends
doctrine/collections                 v1.5.0  Collections Abstraction library
doctrine/common                      v2.6.2  Common Library for Doctrine projects
doctrine/dbal                        v2.5.4  Database Abstraction Layer
doctrine/doctrine-bundle             1.6.8   Symfony DoctrineBundle
doctrine/doctrine-cache-bundle       1.3.0   Symfony Bundle for Doctrine Cache
doctrine/inflector                   v1.2.0  Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator                1.0.5   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                       v1.0.1  Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm                         v2.5.6  Object-Relational-Mapper for PHP

如果您以后遇到此类问题,请转到https://packagist.org/packages/并搜索导致问题的软件包。

例如doctrine/annotationshttps://packagist.org/packages/doctrine/annotations#v1.5.0

然后在那里寻找 (requires: php: ^7.1) 以及这个包是否与您的 PHP 版本匹配。 (在您使用 PHP 7.0 的情况下,它不匹配)

但是 https://packagist.org/packages/doctrine/annotations#v1.4.0 匹配你的 PHP 版本(需要: php: ^5.6 || ^7.0)你可以试试使用它。

为避免此类问题,一个好的做法是设置作曲家 config.platform 设置:

"config": {
    "platform": {
        "php": "7.0.23"
    }
 }

这将告诉作曲家更新包,但仅限于仍然支持此 PHP 版本的版本。因此,通常,此版本号将是您的生产服务器的版本。

只需删除项目中的 composer.lock,"vendor" 文件夹也是如此。

运行 并享受 ->

php composer.phar 自我更新

php composer.phar 安装

确保您的最新 PHP 版本是 运行。 有时,当您使用 php -v 检查 PHP 版本时,它会显示最新安装的版本,但在 运行 中显示较旧的版本。 如果您安装了 PHP 的新版本,只需删除旧版本:

apt purge php7.0 php7.0-common

有同样的问题,在检查了很多东西之后,我的问题通过删除旧版本解决了。