3

I need to create listener or handler to provide some actions for AccessDenied exception.

I've looked some sources of standard handlers and listeners, so looks like they can be use for same task.

I also read this:

  1. What's the difference between Event Listeners & Handlers in Java?
  2. JavaScript - What's the difference between event handlers & listener?
  3. Using Symfony2's AccessDeniedHandlerInterface

But only difference I've seen - that handlers often use for handle exceptions.

So, what the real difference between handler and listener in Symfony ?

Community
  • 1
  • 1
Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59

1 Answers1

5

Listeners are registered and called when an event occurs. Observer or PubSub patterns are used.

The Handler is more of a strategy pattern that delegates implementation details to class. Which can then be substituted. (composition over inheritance)

The patterns could maybe be interchangeable in some cases, and it's more the intention of the code that calls for one or the other then.

NDM
  • 6,731
  • 3
  • 39
  • 52
  • 1
    Important: symfony2 declares the `listener` entity as a part of its Event Dispatcher, but the `handler` term is not officially defined by the framework. So it's not guaranteed that what you stated about the `handler` is true for everything. – zerkms Oct 28 '15 at 21:13
  • 3
    After reading your answer, I still don't understand anything :)) – Anh Nguyen Jul 28 '16 at 19:04