6

I'm actually learning Symfony, regarding the events, in fact I do not see any difference between a simple listener and a subscriber, the two are doing the same thing, but surely, there is a difference while symfony developers have developed two ways for listening to an event.

I did a research to discover this difference but I did not find a clear and convincing answer .

What is the main difference between a listener and a subscriber? Which one is the most used and what is its advantage over the other?And how to know which to use a simple listener or a subscriber?

Medinho
  • 193
  • 1
  • 3
  • 10

1 Answers1

7

Symfony doc

Listeners and subscribers can be used in the same application indistinctly. The decision to use either of them is usually a matter of personal taste. However, there are some minor advantages for each of them:

Subscribers are easier to reuse because the knowledge of the events is kept in the class rather than in the service definition. This is the reason why Symfony uses subscribers internally

Listeners are more flexible because bundles can enable or disable each of them conditionally depending on some configuration value.

Zak
  • 1,005
  • 10
  • 22