且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在事件侦听器中利用 kernel.terminate

更新时间:2023-01-15 15:18:42

你应该注入 symfony 配置的事件调度器服务 (@event_dispatcher) 而不是在监听器中创建一个新的.

You should inject symfony's configured event dispatcher service (@event_dispatcher) instead of creating a new one inside the listener.

如果你只创建它并添加一个事件监听器 symfony 仍然没有引用这个新创建的 EventDispatcher 对象,因此不会使用它.

If you only create it and add an event-listener symfony still has no reference to this newly created EventDispatcher object and therefore won't use it.

<service id="my_service" class="%my_class%">
    <tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
    <argument type="service" id="doctrine.orm.entity_manager" />
    <argument type="service" id="event_dispatcher" />
 </service>