Skip to main content

Events

Custom Events in a Symfony2 Bundle

·301 words·2 mins
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.