Doctrine Listener versus Subscriber

后端 未结 7 949
离开以前
离开以前 2020-12-12 19:52

I\'m working in the Symfony2 framework and wondering when would one use a Doctrine subscriber versus a listener. Doctrine\'s documentation for listeners is very clear, howe

7条回答
  •  粉色の甜心
    2020-12-12 20:10

    From my point of view, there is only one major difference:

    • The Listener is signed up specifying the events on which it listens.
    • The Subscriber has a method telling the dispatcher what events it is listening to

    This might not seem like a big difference, but if you think about it, there are some cases when you want to use one over the other:

    • You can assign one listener to many dispatchers with different events, as they are set at registration time. You only need to make sure every method is in place in the listener
    • You can change the events a subscriber is registered for at runtime and even after registering the subscriber by changing the return value of getSubscribedEvents (Think about a time where you listen to a very noisy event and you only want to execute something one time)

    There might be other differences I'm not aware of though!

提交回复
热议问题