用一个听众听多个事件?

时间:2021-11-14 03:09:10

I'm building an app in Symfony2 that has a social-driven aspect (many actions a user performs on the site will show up in a "news feed"-style list for others to view). I've determined that the sf2 event dispatcher/listener system is the best way to handle this, but I've run into something of a snag in trying to configure my listener to handle many different events.

我正在Symfony2中构建一个具有社交驱动方面的应用程序(用户在网站上执行的许多操作将显示在“新闻提要”样式列表*其他人查看)。我已经确定sf2事件调度程序/侦听器系统是处理这个问题的最佳方法,但是我试图配置我的侦听器来处理许多不同的事件时遇到了一些麻烦。

The (now outdated) documentation I've found in my searches seems to indicate that at one point, event listeners could register on multiple events, but the code has been refactored, and now the configuration looks something like this:

我在搜索中找到的(现在过时的)文档似乎表明,在某一时刻,事件监听器可以注册多个事件,但代码已经重构,现在配置看起来像这样:

config.yml:

config.yml:

services:
  social.listener:
    class: F\Q\C\N\SocialEventListener
    tags:
      - { name: kernel.listener, event: onSocialShare }

Is there any way to either:

有没有办法:

  1. Easily pass multiple events (something like event: [onSocialShare, onSocialFriend, onSocialCreate] works, but that feels like it will quickly get ugly and unmaintainable, clogging up my config file with potentially dozens of social events
  2. 轻松传递多个事件(类似事件:[onSocialShare,onSocialFriend,onSocialCreate],但感觉它会很快变得丑陋且无法维护,可能会破坏我的配置文件中的潜在数十个社交事件
  3. Define the event to which I want to subscribe from code (as was done previously)
  4. 定义我想从代码订阅的事件(如前所述)
  5. Or possibly another, better option that I haven't thought of yet
  6. 或者可能是我还没想过的另一个更好的选择

Thanks in advance.

提前致谢。

2 个解决方案

#1


30  

Word back from the symfony-users google group (thread here) is that the appropriate way to do this is by adding several tags:

从symfony-users google group(此处的主题)回来的话是,通过添加几个标签来实现此目的的恰当方法是:

services:
  social.listener:
    class: F\Q\C\N\SocialEventListener
    tags:
      - { name: kernel.listener, event: onSocialShare }
      - { name: kernel.listener, event: onSocialFriend }
      - { name: kernel.listener, event: ... etc }

So, it looks like currently, there is no good way of adding event subscriptions from listener code. Oh well.

所以,它看起来像目前,没有从侦听器代码添加事件订阅的好方法。好吧。

#2


2  

I think it will be easier to use EventSubscriber, you can define events you want to use directly in that class

我认为使用EventSubscriber会更容易,您可以定义要在该类中直接使用的事件

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

#1


30  

Word back from the symfony-users google group (thread here) is that the appropriate way to do this is by adding several tags:

从symfony-users google group(此处的主题)回来的话是,通过添加几个标签来实现此目的的恰当方法是:

services:
  social.listener:
    class: F\Q\C\N\SocialEventListener
    tags:
      - { name: kernel.listener, event: onSocialShare }
      - { name: kernel.listener, event: onSocialFriend }
      - { name: kernel.listener, event: ... etc }

So, it looks like currently, there is no good way of adding event subscriptions from listener code. Oh well.

所以,它看起来像目前,没有从侦听器代码添加事件订阅的好方法。好吧。

#2


2  

I think it will be easier to use EventSubscriber, you can define events you want to use directly in that class

我认为使用EventSubscriber会更容易,您可以定义要在该类中直接使用的事件

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html

http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html