Custom Events in Symfony2 Bundle

In this tutorial we will create a custom event for symfony2 bundle.

Assuming you have downloaded the symfony-standard distribution to play.

Create HktEventBundle via sensio generator bundle.

1
php app/console generate:bundle --namespace=Hkt/EventBundle --dir src --no-interaction

Create the event class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
// src/Hkt/EventBundle/Event/PageViewed.php
namespace Hkt\EventBundle\Event;

use Symfony\Component\EventDispatcher\Event;

class PageViewed extends Event
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }
}

Add as many methods/properties which are needed from the listener. Instead of creating an event class we can make use of generic event also.

Dispatching Event

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
//src/Hkt/EventBundle/Controller/DefaultController.php
namespace Hkt\EventBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Hkt\EventBundle\Event\PageViewed;

class DefaultController extends Controller
{
    /**
     * @Route("/hello/{name}")
     * @Template()
     */
    public function indexAction($name)
    {
        $event = new PageViewed($name);
        $this->get('event_dispatcher')->dispatch('hkt.event.page_viewed', $event);
        return array('name' => $name);
    }
}

Listener

Create the listener to do what you want to do with the dispatched/triggered event.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
// src/Hkt/EventBundle/EventListener/PageViewedListener.php
namespace Hkt\EventBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Hkt\EventBundle\Event\PageViewed;

class PageViewedListener implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            'hkt.event.page_viewed' => 'handler',
        );
    }

    public function handler(PageViewed $event)
    {
        // $event->getName();
        // do what you want with the event
    }
}

In order to get the PageViewedListener to be called, we need to register the listener to the event_dispatcher.

Add the below lines in services.yml or services.xml accordingly.

1
2
3
4
5
# src/Hkt/EventBundle/Resources/config/services.yml
hkt.event.page_viewed:
    class: Hkt\EventBundle\EventListener\PageViewedListener
    tags:
        - {name: kernel.event_listener, event:hkt.event.viewed, method:handler} 

or in xml as

1
2
3
4
5
<services>
    <service id="hkt.event.page_viewed" class="Hkt\EventBundle\EventListener\PageViewedListener">
        <tag name="kernel.event_listener" event="hkt.event.page_viewed" method="handler"/>
    </service>
</services>

There may be better ways to do this. If you think so do share your knowledge.

Thank you.

bundle, event-dispatcher, symfony2

Comments