Symfony4.2升4.4报错:无扩展可加载doctrine_cache配置
Symfony 4.2升级4.4后doctrine_cache配置加载致命错误修复方案
问题背景
- 项目从Symfony 4.2升级到4.4版本后,执行项目迭代变更操作时触发致命错误,升级前所有功能可正常运行
- 运行环境版本:PHP 7.3.19
核心报错
Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\InvalidArgumentException: There is no extension able to load the configuration for "doctrine_cache" (in "/var/www/intranet/config/packages/doctrine_cache.yaml"). Looked for namespace "doctrine_cache", found ""framework", "sensio_framework_extra", "doctrine", "doctrine_migrations", "security", "swiftmailer", "twig", "monolog", "knp_paginator"". in /var/www/intranet/vendor/symfony/config/Loader/FileLoader.php on line 180
故障原因
升级依赖时doctrine/doctrine-bundle被升级到了2.7版本,该版本从2.0开始就已经移除了对doctrine/doctrine-cache-bundle的强制依赖,也不再自动注册doctrine_cache配置命名空间。
项目中残留的旧版配置文件config/packages/doctrine_cache.yaml没有对应的Bundle提供配置解析能力,就会触发该报错。之前手动注册Doctrine Cache相关Bundle时报类不存在错误,是因为项目本身没有安装doctrine/doctrine-cache-bundle这个依赖包。
修复方案
方案1:适配新版本Doctrine Bundle(官方推荐)
- 直接删除旧版冗余配置文件:删除路径
config/packages/doctrine_cache.yaml - 将原本写在
doctrine_cache.yaml里的缓存配置,迁移到config/packages/doctrine.yaml的doctrine配置节点下,示例配置:
doctrine: orm: metadata_cache_driver: type: pool pool: doctrine.system_cache_pool query_cache_driver: type: pool pool: doctrine.system_cache_pool result_cache_driver: type: pool pool: doctrine.result_cache_pool
- 执行命令清理项目缓存:
php bin/console cache:clear
方案2:临时兼容旧配置(仅过渡使用)
如果暂时不想调整原有缓存配置,可以手动安装缺失的依赖兼容旧逻辑:
- 执行composer命令安装依赖:
composer require doctrine/doctrine-cache-bundle
- 在
config/bundles.php中注册对应Bundle:
Doctrine\Bundle\DoctrineCacheBundle\DoctrineCacheBundle::class => ['all' => true],
- 执行缓存清理命令:
php bin/console cache:clear
附:当前项目相关配置
composer.json核心依赖片段
{ "require": { "php": "^7.1.3", "ext-ctype": "*", "ext-iconv": "*", "doctrine/annotations": "^1.0", "doctrine/doctrine-bundle": "^2.7", "doctrine/doctrine-migrations-bundle": "^3.2", "doctrine/orm": "^2.10", "knplabs/knp-paginator-bundle": "^3.0", "phpdocumentor/reflection-docblock": "^5.3", "phpoffice/phpspreadsheet": "^1.20", "sensio/framework-extra-bundle": "^5.1", "symfony/apache-pack": "^1.0", "symfony/asset": "4.4.*", "symfony/console": "4.4.*", "symfony/dotenv": "4.4.*", "symfony/expression-language": "4.4.*", "symfony/flex": "^1.1", "symfony/form": "4.4.*", "symfony/framework-bundle": "4.4.*", "symfony/monolog-bundle": "^3.1", "symfony/process": "4.4.*", "symfony/property-access": "4.4.*", "symfony/property-info": "4.4.*", "symfony/proxy-manager-bridge": "4.4.*", "symfony/security-bundle": "4.4.*", "symfony/serializer": "4.4.*", "symfony/swiftmailer-bundle": "^3.1", "symfony/templating": "4.4.*", "symfony/translation": "4.4.*", "symfony/twig-bundle": "4.4.*", "symfony/validator": "4.4.*", "symfony/web-link": "4.4.*", "symfony/yaml": "4.4.*" } }
bundles.php已注册Bundle列表
return [ Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true], Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true], Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true], Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true], Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true], Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true], Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true], Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true], Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true], Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true], ];
内容的提问来源于stack exchange,提问作者Gabriel Briziou
相关产品推荐
相关产品推荐

