Doctrine Listener versus Subscriber

后端 未结 7 963
离开以前
离开以前 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 19:53

    Here is what the doc is saying about that in 4.1. As this is globally applied to events, I suppose it's also valid for Doctrine (not 100% sure).

    Listeners or Subscribers

    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.

    http://symfony.com/doc/master/event_dispatcher.html#listeners-or-subscribers

提交回复
热议问题