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:
有没有办法:
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 eventsThanks in advance.
提前致谢。
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
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
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/05/09/6f49663de7347028f4cc31c1176b3b0e.html。