Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 event subscriber does not call listeners

I am trying to set up a simple event subscription based on the example given here - http://symfony.com/doc/master/components/event_dispatcher/introduction.html.

Here's my event store:

namespace CookBook\InheritanceBundle\Event;

final class EventStore
{
    const EVENT_SAMPLE = 'event.sample';
}

Here's my event subscriber:

namespace CookBook\InheritanceBundle\Event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;

class Subscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        var_dump('here');
        return array(
                'event.sample' => array(
                        array('sampleMethod1', 10),
                        array('sampleMethod2', 5)
                ));
    }

    public function sampleMethod1(Event $event)
    {
        var_dump('Method 1');
    }

    public function sampleMethod2(Event $event)
    {
        var_dump('Method 2');
    }
}

Here's the config in services.yml:

kernel.subscriber.subscriber:
    class: CookBook\InheritanceBundle\Event\Subscriber
    tags:
        - {name:kernel.event_subscriber}

And here's how I raise the event:

use Symfony\Component\EventDispatcher\EventDispatcher;
use CookBook\InheritanceBundle\Event\EventStore;
$dispatcher = new EventDispatcher();
$dispatcher->dispatch(EventStore::EVENT_SAMPLE);

Expected output:

string 'here' (length=4)
string 'Method 1' (length=8)
string 'Method 2' (length=8)

Actual output:

string 'here' (length=4)

For some reason, the listener methods don't get called. Anyone knows what's wrong with this code? Thanks.

like image 883
Prathap Avatar asked Aug 28 '13 13:08

Prathap


2 Answers

What @Tristan said. The tags portion in your services file is part of the Symfony Bundle and is only processed if you pull the dispatcher out of the container.

Your example will work as expected if you do this:

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new Subscriber());
$dispatcher->dispatch(EventStore::EVENT_SAMPLE);
like image 147
Cerad Avatar answered Oct 19 '22 10:10

Cerad


You might try to inject a configured EventDispatcher (@event_dispatcher) instead of instanciating a new one (new EventDispatcher)

If you only create it and add an event-listener Symfony still has no reference to this newly created EventDispatcher object and won't use it.

If you are inside a controller who extends ContainerAware :

use Symfony\Component\EventDispatcher\EventDispatcher;
use CookBook\InheritanceBundle\Event\EventStore;

...

$dispatcher = $this->getContainer()->get('event_dispatcher');
$dispatcher->dispatch(EventStore::EVENT_SAMPLE);

I've adapted my answer thanks to this question's answer even though the context of both questions are different, the answer still applies.

like image 35
sf_tristanb Avatar answered Oct 19 '22 10:10

sf_tristanb