我有一个简单的监听器示例 https://symfony.com/doc/current/event_dispatcher.html#content_wrapper
示例是1:1
services.yml是相同的
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
# AppBundle\:
# resource: '../../src/AppBundle/*'
# # you can exclude directories or files
# # but if a service is unused, it's removed anyway
# exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
# AppBundle\Controller\:
# resource: '../../src/AppBundle/Controller'
# public: true
# tags: ['controller.service_arguments']
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
但是,监听器被列为"未被监听的听众"
我做错了什么?
答案 0 :(得分:2)
如果一个类实现了给定的接口,则可以自动标记它 - 这就是EventSubscriber示例的情况。如果侦听器没有提示容器构建器(接口或它扩展的类),那么就无法知道它应该被标记为侦听器,或者是哪些事件。
您可能希望在配置中显式标记侦听器,如示例所示。
# app/config/services.yml
services:
AppBundle\EventListener\ExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception }
可以推断出订阅者,因为它在事件getSubscribedEvents()
之间有明确的映射,如KernelEvents::EXCEPTION
和要运行的类方法。
正如The Symfony 3.3 DI Container Changes Explained
所述不适用于所有代码。许多标签都有必需的属性,例如事件监听器,您还需要在标记中指定事件名称和方法。自动配置仅适用于没有任何必需标记属性的标记