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

TYPO3中如何使用EventSubscriber监听symfony/workflow事件

TYPO3中使用Symfony EventSubscriber监听字符串事件解决方案

问题根因

  1. TYPO3默认的event.listener标签仅支持事件类全限定名作为事件标识,无法适配symfony/workflow使用的字符串事件名规则。
  2. TYPO3核心默认没有为Symfony\Component\EventDispatcher\EventSubscriberInterface配置自动标签规则,所以即使开启了autoconfigure,订阅者也不会被自动注册到事件调度器。

解决步骤

步骤1:配置扩展的Services.yaml

在你的扩展Configuration/Services.yaml中添加自动标签规则,让TYPO3自动识别Symfony风格的事件订阅者:

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  # 全局配置:所有实现Symfony事件订阅者接口的类自动打注册标签
  Symfony\Component\EventDispatcher\EventSubscriberInterface:
    tags: ['event.subscriber']

  # 你的订阅者类无需额外配置标签,只要实现接口即可自动识别
  Vendor\MyExt\EventListener\MyWorkflowEventSubscriber: ~

步骤2:编写事件订阅者类

在订阅者的getSubscribedEvents方法中直接用字符串事件名作为键配置监听规则,同时支持设置优先级,完美适配工作流的事件体系:

<?php
declare(strict_types=1);

namespace Vendor\MyExt\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\LeaveEvent;
use Symfony\Component\Workflow\Event\EnterEvent;

class MyWorkflowEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            // 直接填写字符串事件名,对应处理方法
            'workflow.myname.leave' => 'onLeave',
            'workflow.myname.enter' => 'onEnter',
            // 第二位数组元素为事件优先级,数值越高执行越早
            'workflow.myname.transition' => ['onTransition', 10],
        ];
    }

    public function onLeave(LeaveEvent $event): void
    {
        // 处理leave事件的业务逻辑
        $subject = $event->getSubject();
        $transition = $event->getTransition();
    }

    public function onEnter(EnterEvent $event): void
    {
        // 处理enter事件的业务逻辑
    }

    public function onTransition(LeaveEvent $event): void
    {
        // 处理transition事件的业务逻辑
    }
}

步骤3:验证生效

修改完成后清空TYPO3全部缓存,订阅者会自动注册到事件调度器,即可正常监听所有工作流抛出的字符串命名事件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 18:06:05