Shopware App启动报错求助:依赖缺失问题排查
问题背景
作为Shopware和Symfony新手,按照官方文档开发应用时,执行composer require shopware/app-bundle后出现两次报错:
第一个报错(Doctrine依赖缺失)
The service "Shopware\App\SDK\Shop\ShopRepositoryInterface" has a dependency on a non-existent service "Doctrine\Persistence\ManagerRegistry"
手动创建doctrine.yaml后该报错消失,但出现第二个报错:
第二个报错(PSR-7工厂缺失)
Cannot autowire service "Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface": argument "$serverRequestFactory" of method "Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory::__construct()" references interface "Psr\Http\Message\ServerRequestFactoryInterface" but no such service exists. Did you create a class that implements this interface?
解决方案
1. 安装缺失的核心依赖
执行以下Composer命令,补充Doctrine ORM和PSR-7消息实现的依赖:
composer require doctrine/orm symfony/psr-http-message-bridge nyholm/psr7
doctrine/orm:提供ManagerRegistry服务,解决第一个依赖问题symfony/psr-http-message-bridge:实现Symfony与PSR-HTTP消息规范的桥接nyholm/psr7:提供PSR-7消息接口的具体实现,包含ServerRequestFactoryInterface等所需服务
2. 完善Doctrine配置(若手动创建的配置不完整)
确保config/packages/doctrine.yaml包含基础配置示例:
doctrine: dbal: url: '%env(resolve:DATABASE_URL)%' driver: 'pdo_mysql' server_version: '8.0' charset: utf8mb4 orm: auto_generate_proxy_classes: true naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware auto_mapping: true mappings: App: is_bundle: false dir: '%kernel.project_dir%/src/Entity' prefix: 'App\Entity' alias: App
同时检查.env文件中是否配置了有效的数据库连接:
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name?serverVersion=8.0&charset=utf8mb4"
3. 验证服务可用性
执行Symfony容器调试命令,确认所需服务已注册:
php bin/console debug:container Doctrine\Persistence\ManagerRegistry php bin/console debug:container Psr\Http\Message\ServerRequestFactoryInterface
若命令返回对应的服务信息,说明依赖和配置已生效。
4. 刷新依赖并清除缓存
composer dump-autoload php bin/console cache:clear
原因说明
官方文档默认基于Symfony标准Web应用模板编写(该模板已预装这些核心依赖),但如果是从极简Symfony项目(如symfony new --webapp以外的模板)创建的项目,就需要手动补充这些Shopware App Bundle依赖的基础组件。
内容的提问来源于stack exchange,提问作者Moss

