Question

I have a subscriber which implements the EventSubscriberInterface. However, in the Event dispatcher component documentation, it is said that to subscribe to an event, I should call $dispatcher->addSubscriber().

Now, for my example, imagine you have a Mailer class which subscribes to some events and sends e-mails. My basic question is, where can I add this to the application so that this object will be added as a listener application-wide? In other terms, where should I call $dispatcher->addSubscriber() that is bound to run before the event happens?

Was it helpful?

Solution

I am repeating myself over and over again:

Dev's rule number 1 (one) : Man pages are the programmer's best friend

Everything you need to know is right here in Symfony Docs, the section you are interested in follows:

Configuring the Listener/Subscriber

Now that the class is created, you just need to register it as a service and notify Symfony that it is a "listener" on the kernel.exception event by using a special "tag": YAML

# app/config/config.yml
services:
    kernel.listener.your_listener_name:
        class: Acme\DemoBundle\EventListener\AcmeExceptionListener
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top