You need to enable JavaScript to run this app. Symfony/Shopware:EventSubscriberInterface的容器注入与服务获取
优惠活动
大模型
产品
解决方案
定价
更多

Symfony/Shopware:EventSubscriberInterface的容器注入与服务获取

问题根因

你当前代码存在几个直接导致功能失效的问题:

  • 代码里直接调用getContainer()是PHP不存在的全局函数,运行时会直接触发错误,根本无法获取服务实例
  • 你虽然定义了setContainer、getContainer方法,但类本身没有实现ContainerAwareInterface接口,容器不会自动执行这个方法注入实例
  • 依赖关系判断失误:dump($event)里找不到容器是正常的,事件对象只携带业务上下文数据,服务容器是服务层的注入依赖,不会作为事件属性暴露出来
最佳实践方案

完全不需要把整个容器注入到事件订阅器里,你只需要两个仓储服务,直接通过构造函数显式注入即可,这也是Symfony官方推荐的依赖注入方式,能让代码依赖关系更清晰,减少不必要的代理加载开销。

第一步:改造订阅器类

删除冗余的容器相关方法、无用的继承引用,直接把需要的两个仓储作为类属性:

<?php
namespace RegistrationExtension\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class RegisterExtensionSubscriber implements EventSubscriberInterface
{
    private EntityRepositoryInterface $mailTemplateTypeRepository;
    private EntityRepositoryInterface $mailTemplateRepository;

    // 构造函数直接接收注入的服务实例
    public function __construct(
        EntityRepositoryInterface $mailTemplateTypeRepository,
        EntityRepositoryInterface $mailTemplateRepository
    ) {
        $this->mailTemplateTypeRepository = $mailTemplateTypeRepository;
        $this->mailTemplateRepository = $mailTemplateRepository;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            AccountLoginPageLoadedEvent::class => 'onCustomRegister'
        ];
    }

    public function onCustomRegister(AccountLoginPageLoadedEvent $event)
    {
        // 直接使用类属性里的仓储实例即可
        $mailTypeEntity = $this->mailTemplateTypeRepository->search(
            (new Criteria())->addFilter(new EqualsFilter('mail_template_type.technicalName', "customer_register")),
            $event->getContext()
        );

        $mailTypeId = $mailTypeEntity->getEntities()->first()?->getId();
        if (!$mailTypeId) {
            return;
        }

        $mailEntity = $this->mailTemplateRepository->search(
            (new Criteria())->addFilter(new EqualsFilter('mail_template.mailTemplateTypeId', $mailTypeId)),
            $event->getContext()
        );

        $mail = $mailEntity->getEntities();
        // 后续业务逻辑处理
    }
}

第二步:调整服务配置

由于这两个仓储服务的ID和构造函数的类型提示不是自动匹配的(都是EntityRepositoryInterface类型),需要在服务定义里显式把对应服务实例传入构造函数:

<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="RegistrationExtension\Storefront\Controller\RegistrationExtensionController" public="true" />
        
        <service id="RegistrationExtension\Subscriber\RegisterExtensionSubscriber">
            <tag name="kernel.event_subscriber"/>
            <!-- 按构造函数顺序传入两个仓储服务 -->
            <argument type="service" id="mail_template_type.repository" />
            <argument type="service" id="mail_template.repository" />
        </service>
    </services>
</container>

注意:如果你的项目使用PHP版本配置而不是XML配置,对应在services.yaml里给这个订阅器配置arguments即可,逻辑完全一致。

关于注入整个容器的特殊场景

如果有极特殊的需求非要拿到完整容器实例,不建议在EventSubscriber中使用这种方式——容易引入隐性依赖、增加调试难度。真要实现的话需要:

  1. 让订阅器类实现Symfony\Component\DependencyInjection\ContainerAwareInterface接口
  2. 在服务配置中把容器实例作为参数注入到订阅器中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.26 11:15:06