本文介绍了主义听众与订阅者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Symfony2框架中工作,想知道什么时候使用Doctrine订阅者和收听者。 Doctrine的非常清楚,但订阅者相当掩盖了Symfony的是类似的。
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, however subscribers are rather glossed over. Symfony's cookbook entry is similar.
推荐答案
从我的观点来看,只有一个主要区别:
From my point of view, there is only one major difference:
- 指定其侦听的事件。
- 订阅者有一种方法告诉调度员哪些事件正在收听
这可能不是一个很大的区别,但如果你想到,有一些情况下你想要使用一个:
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:
- 您可以为具有不同事件的多个调度程序分配一个监听器,因为它们在注册时设置。您只需要确保每个方法在侦听器中就位
- 您可以在运行时更改订户注册的事件,甚至在注册用户之后,通过更改
getSubscribedEvents
(想想你听一个非常嘈杂的事件的时间,你只想执行一次)
- 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!
这篇关于主义听众与订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!