Shopware 6.5插件升级遇依赖注入错误:无法自动装配ContainerInterface
Shopware 6.5插件升级依赖注入错误解决
错误提示
无法自动装配服务"**\Controller*ProductController":方法"Symfony\Bundle\FrameworkBundle\Controller\AbstractController::setContainer()"的参数"$container"引用了接口"Psr\Container\ContainerInterface",但该服务不存在。
该接口可用的自动注入别名是:"$parameterBag"。
相关代码与配置
抽象控制器类
use Shopware\Core\System\SystemConfig\SystemConfigService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; abstract class AMyController extends AbstractController { public function __construct(private readonly SystemConfigService $systemConfigService) { } //... }
业务控制器类
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\System\SystemConfig\SystemConfigService; use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder; use Symfony\Component\Routing\Annotation\Route; #[Route(defaults: ['_routeScope' => ['api']])] class MyProductController extends AMyController { public function __construct( private readonly EntityRepository $orderRepository, private readonly EntityRepository $orderLineItemRepository, private readonly EntityRepository $productRepository, private readonly RequestCriteriaBuilder $requestCriteriaBuilder, private readonly SystemConfigService $systemConfigService ) { parent::__construct($systemConfigService); } //... }
services.xml配置
<services> <service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" /> <service id="MyPluginName\Controller\MyProductController" public="true"> <argument type="service" id="order.repository"/> <argument type="service" id="order_line_item.repository"/> <argument type="service" id="product.repository"/> <argument type="service" id="Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder"/> <argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/> <call method="setContainer"> <argument type="service" id="service_container"/> </call> </service> </services>
composer.json配置
{ "autoload": { "psr-4": { "MyPrefix\MyPluginName\": "src/" } }, "require": { "shopware/core": ">=v6.5.0", "shopware/administration": ">=v6.5.0" } }
当前使用版本
- Shopware 6.5.2.1
- Symfony 6.3.0
- PHP 8.1.20
已尝试的操作
- 改用Route注解
#[Route(defaults: ['_routeScope' => ['api']])] - 在services.xml中手动定义所有服务
- 执行
./bin/console cache:clear清除缓存
解决方案
问题根源
Shopware 6.5基于Symfony 6,Symfony 6中**Psr\Container\ContainerInterface不再默认作为可自动注入的服务**,同时你手动配置的ContainerInterface别名和setContainer调用属于冗余操作——继承AbstractController的控制器,Symfony会自动处理容器注入,无需手动调用setContainer。
具体修复步骤
清理services.xml冗余配置
移除以下无效别名配置:<service id="Symfony\Component\DependencyInjection\ContainerInterface" alias="service_container" />同时删除控制器服务定义中的
setContainer调用块:<call method="setContainer"> <argument type="service" id="service_container"/> </call>优化服务配置(推荐启用自动注入)
Shopware 6.5支持控制器自动注册和依赖注入,可完全移除手动的控制器服务定义,改用自动扫描:<services> <defaults autowire="true" autoconfigure="true" public="false"/> <prototype namespace="MyPluginName\" resource="../src/*"/> <!-- 若需保留手动定义,确保不要手动调用setContainer --> </services>此配置会自动扫描插件src目录下的所有类并启用自动注入,减少手动维护成本。
修正composer.json命名空间转义
autoload配置中的反斜杠未正确转义,需修改为:"psr-4": { "MyPrefix\\MyPluginName\\": "src/" }重新清理缓存并刷新插件
执行以下命令:./bin/console cache:clear ./bin/console plugin:refresh ./bin/console plugin:install -r YourPluginName composer dump-autoload
额外说明
- 继承
AbstractController的控制器无需手动注入容器,Symfony会通过控制器解析器自动处理setContainer调用。 - 若业务中确实需要容器实例,应注入
Symfony\Component\DependencyInjection\ContainerInterface而非Psr\Container\ContainerInterface,但控制器场景下完全不需要手动处理。
内容的提问来源于stack exchange,提问作者Drayke
相关产品推荐
相关产品推荐

