Discussion:
[Symfony2] listening to multiple events with one listener?
Problematic
2011-05-09 18:24:54 UTC
Permalink
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.

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:

services:
social.listener:
class: F\Q\C\N\SocialEventListener
tags:
- { name: kernel.listener, event: onSocialShare }
Is there any way to either:

* 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

* Define the event to which I want to subscribe from code (as was done
previously)

* Or possibly another, better option that I haven't thought of yet

Thanks in advance.
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Christophe COEVOET
2011-05-09 18:33:02 UTC
Permalink
Post by Problematic
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.
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
class: F\Q\C\N\SocialEventListener
- { name: kernel.listener, event: onSocialShare }
* 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
* Define the event to which I want to subscribe from code (as was done
previously)
* Or possibly another, better option that I haven't thought of yet
Thanks in advance.
Just put several tags:

services:
my_listener:
tags:
- { name: kernel.listener, event: onCoreRequest }
- { name: kernel.listener, event: onCoreResponse }
--
Christophe | Stof
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Problematic
2011-05-09 18:46:44 UTC
Permalink
Thanks, Christophe! Guess I'll just handle my event list that way,
then.

As a related aside, is there any way to automatically register a
subscriber (I'm reading about it here:
http://symfony.com/doc/current/book/internals/event_dispatcher.html#using-event-subscribers),
like you can do with a listener? It seems like if I can automatically
notify the dispatcher of a list of events that can be defined in code,
that would be more flexible and fit what I'm trying to do more
completely.
Post by Problematic
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.
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
     class: F\Q\C\N\SocialEventListener
       - { name: kernel.listener, event: onSocialShare }
* 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
* Define the event to which I want to subscribe from code (as was done
previously)
* Or possibly another, better option that I haven't thought of yet
Thanks in advance.
                - { name: kernel.listener, event: onCoreRequest }
                - { name: kernel.listener, event: onCoreResponse }
--
Christophe | Stof
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Christophe COEVOET
2011-05-09 18:52:31 UTC
Permalink
Post by Problematic
Thanks, Christophe! Guess I'll just handle my event list that way,
then.
As a related aside, is there any way to automatically register a
http://symfony.com/doc/current/book/internals/event_dispatcher.html#using-event-subscribers),
like you can do with a listener? It seems like if I can automatically
notify the dispatcher of a list of events that can be defined in code,
that would be more flexible and fit what I'm trying to do more
completely.
There is no easy way to add a subscriber through the configuration of
your services AFAIK. And this does not make much sense anyway as a
subscriber cannot be lazy-loaded whereas a listener can.
--
Christophe | Stof
--
If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to symfony-users-/***@public.gmane.org
To unsubscribe from this group, send email to
symfony-users+unsubscribe-/***@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
Continue reading on narkive:
Loading...