Skip to main content

Custom Events in a Symfony2 Bundle

·301 words·2 mins
Hari KT
Author
Hari KT
Freelancer | Founder of Tripti and Tanvish | Maintainer of Aura for PHP, created by Paul M Jones
Table of Contents

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
2
<?php
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
23
<?php
<?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
# src/Hkt/EventBundle/Resources/config/services.yml
hkt.event.page_viewed:
    class: Hkt\EventBundle\EventListener\PageViewedListener
Tags = [

Related

20 Years of PHP

·302 words·2 mins
First my apologies being late! I hope you all know PHP is celebrating its 20th years of existence in the web. There is something that makes PHP unique that helps to standout with other languages. In this opporchunity I would like to thank the creator, the contributors, the maintainers, and all the users (past/present) to make this memorable. My Story # I got an exposure to learn and use internet in my college days. In those period I look html as a very complex language. I wasn’t aware there exists editors which help you.

Zend Feed and Guzzle

·348 words·2 mins
You may have worked with Zend Feed as a standalone component. I don’t know whether you have integrated Zend framework Feed with Guzzle as Http Client. This post is inspired by Matthew Weier O’Phinney, who have mentioned the same on github. Our composer.json looks 1 2 3 4 5 6 7 8 9 10 11 12 { "require": { "guzzlehttp/guzzle": "~5.2", "zendframework/zend-feed": "~2.3", "zendframework/zend-servicemanager": "~2.3" }, "autoload": { "psr-0": { "": "src/" } } } Zen\Feed\Reader\Reader have a method importRemoteFeed which accepts an instance of Zend\Feed\Reader\Http\ClientInterface.

Conduit: Middleware for PHP

·618 words·3 mins
Long back, I happened to talk with Beau Simensen about stackphp on #auraphp channel. It was hard for me to digest when I noticed it need symfony/http-kernel and its dependencies. After a few months, I started to like the middleware approach of slim framework and wanted to push it to aura. But nothing happened there. Conduit to rescue # Conduit is a Middleware for PHP built by Matthew Weier O’Phinney lead of Zend framework. Conduit supports the current PSR-7 proposal. I believe like the many PSR’s, PSR-7 will be a revolution in the PHP world. Conduit is really a micro framework and can grow with your project.