You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Drupal 8.9.2升级至9.5.9时运行update.php遇致命错误求助

Drupal 8.9.2 升级至9.5.9 出现ServiceNotFoundException 问题解决

问题描述

尝试将现有Drupal网站从8.9.2版本升级至9.5.9版本,运行update.php时出现以下错误:

The website encountered an unexpected error. Please try again later.
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "router.route_provider" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead. in Symfony\Component\DependencyInjection\Container->make() (line 275 of vendor/symfony/dependency-injection/Container.php)

已从新下载的Drupal目录中复制了core和vendor文件夹以及所有根级文件,但运行update.php时仍报错。此前将Drupal 7升级至8.9.2时未遇到问题。
服务器环境:Apache: 2.4,PHP: 7.2,MySQL: 5.7

问题原因

Drupal 9 对服务容器机制做了调整,router.route_provider服务默认不再设为公开服务。网站中若存在自定义模块、主题或第三方模块直接通过容器获取该服务(比如使用\Drupal::getContainer()->get('router.route_provider')这类代码),就会触发该错误。另外,直接复制core和vendor的升级方式,容易遗漏依赖检查、模块兼容性验证等关键步骤。

解决步骤

  • 验证模块兼容性
    1. 确认所有第三方模块、自定义模块均支持Drupal 9.5.9,可通过本地运行composer show查看模块版本,或核对模块官方兼容说明。
    2. 移除或替换不兼容模块,重点排查直接调用容器服务的老旧模块。
  • 修复自定义代码的服务调用方式
    1. 搜索自定义模块/主题代码,定位所有直接使用\Drupal::getContainer()->get('router.route_provider')的位置,改为依赖注入方式引入router.route_provider服务(或对应的\Drupal\Core\Routing\RouteProviderInterface接口)。
    2. 示例(自定义控制器中注入服务):
      use Drupal\Core\Routing\RouteProviderInterface;
      use Drupal\Core\Controller\ControllerBase;
      use Symfony\Component\DependencyInjection\ContainerInterface;
      
      class MyCustomController extends ControllerBase {
        protected $routeProvider;
      
        public function __construct(RouteProviderInterface $route_provider) {
          $this->routeProvider = $route_provider;
        }
      
        public static function create(ContainerInterface $container) {
          return new static(
            $container->get('router.route_provider')
          );
        }
      
        // 后续通过$this->routeProvider调用服务方法
      }
      
  • 改用标准升级流程(替代手动复制)
    1. 推荐使用Composer完成升级,避免依赖遗漏:
      • 执行命令:composer require drupal/core-recommended:9.5.9 drupal/core-composer-scaffold:9.5.9 drupal/core-project-message:9.5.9 --update-with-all-dependencies
      • 依赖更新完成后,运行drush updatedb或浏览器访问update.php执行数据库更新
    2. 若坚持手动升级:
      • 清空sites/default/files/php目录下的容器缓存文件
      • 确保根目录所有文件(如autoload.php、composer.json等)完全替换为Drupal 9.5.9版本
      • 运行composer dump-autoload更新自动加载规则
  • 升级PHP版本
    Drupal 9.5.x最低要求PHP 7.3,当前服务器使用的PHP 7.2不符合要求,建议升级至PHP 7.3及以上版本(推荐7.4或8.0,符合Drupal 9官方支持范围)。

额外注意事项

  • 升级前务必完成网站文件和数据库的完整备份,防止数据丢失。
  • 优先在本地测试环境完成升级验证,再部署至生产服务器。

内容的提问来源于stack exchange,提问作者Souvik Das

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:35:28