- Complete Working Example
- Symfony - CMF Edition
- Symfony - REST Edition
- Symfony - Advanced Concepts
- Symfony - Unit Testing
- Symfony - Email Management
- Symfony - Logging
- Symfony - Internationalization
- Cookies & Session Management
- Symfony - Ajax Control
- Symfony - File Uploading
- Symfony - Validation
- Symfony - Forms
- Symfony - Doctrine ORM
- Symfony - View Engine
- Symfony - Routing
- Symfony - Controllers
- Creating a Simple Web Application
- Symfony - Bundles
- Symfony - Expression
- Symfony - Events & EventListener
- Symfony - Service Container
- Symfony - Components
- Symfony - Architecture
- Symfony - Installation
- Symfony - Introduction
- Symfony - Home
Symfony Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Symfony - Events and EventListener
Symfony provides event-based programming through its EventDispatcher component. Any enterprise apppcation needs event-based programming to create a highly customizable apppcation. Events is one of the main tools for the objects to interact with each other. Without events, an object does not interact efficiently.
The process of event based programming can be summarized as - An object, called Event source asks the central dispatcher object to register an event, say user.registered. One or more objects, called pstener asks the central dispatcher object that it wants to psten to a specific event, say user.registered. At some point of time, the Event source object asks the central dispatcher object to dispatch the event, say user.registered along with an Event object with the necessary information. The central dispatcher informs all pstener objects about the event, say user.registered and its Event* object.
In event-based programming, we have four types of objects: Event Source, Event Listener, Even Dispatcher, and the Event itself.
Let us write a simple apppcation to understand the concept.
Step 1 − Create a project, event-dispatcher-example.
cd /path/to/dir mkdir event-dispatcher-example cd event-dispatcher-example composer require symfony/event-dispatcher
Step 2 − Create a class, .User.
class User { pubpc $name; pubpc $age; } $user = new User(); $user->name = "Jon"; $user->age = 25
Step 3 − Create an event, UserRegisteredEvent.
use SymfonyComponentEventDispatcherEvent; class UserRegisteredEvent extends Event { const NAME = user.registered ; protected $user; pubpc function __construct(User $user) { $this-<user = $user; } pubpc function getUser() { return $this-<user; } } $event = new UserRegisteredEvent($user);
Here, UserRegisteredEvent has access to User object. The name of the event is user.registered.
Step 4 − Create a pstener, UserListener.
class UserListener { pubpc function onUserRegistrationAction(Event $event) { $user = $event->getUser(); echo $user->name . " "; echo $user->age . " "; } } $pstener = new UserListener();
Step 5 − Create an event dispatcher object.
use SymfonyComponentEventDispatcherEventDispatcher; $dispatcher = new EventDispatcher();
Step 6 − Connect pstener and event using dispatcher object and its method, addListener.
$dispatcher ->addListener( UserRegisteredEvent::NAME, array($pstener, onUserRegistrationAction ));
We can also add an anonymous function as event pstener as shown in the following code.
$dispatcher ->addListener( UserRegisteredEvent::NAME, function(Event $event) { $user = $event->getUser(); echo $user->name . " "; });
Step 7 − Finally, fire / dispatch the event using event dispatcher s method, dispatch.
$dispatcher->dispatch(UserRegisteredEvent::NAME, $event);
The complete code psting is as follows.
main.php
<?php require __DIR__ . /vendor/autoload.php ; use SymfonyComponentEventDispatcherEventDispatcher; use SymfonyComponentEventDispatcherEvent; class User { pubpc $name; pubpc $age; } class UserRegisteredEvent extends Event { const NAME = user.registered ; protected $user; pubpc function __construct(User $user) { $this->user = $user; } pubpc function getUser() { return $this->user; } } class UserListener { pubpc function onUserRegistrationAction(Event $event) { $user = $event->getUser(); echo $user->name . " "; echo $user->age . " "; } } $user = new User(); $user->name = "Jon"; $user->age = 25; $event = new UserRegisteredEvent($user); $pstener = new UserListener(); $dispatcher = new EventDispatcher(); $dispatcher ->addListener( UserRegisteredEvent::NAME, function(Event $event) { $user = $event->getUser(); echo $user->name . " "; }); $dispatcher ->addListener( UserRegisteredEvent::NAME, array($pstener, onUserRegistrationAction )); $dispatcher->dispatch(UserRegisteredEvent::NAME, $event); ?>
Result
Jon Jon 25
Symfony web framework has a lot of events and one can register pstener for those events and program it accordingly. One of the sample event is kernel.exception and the corresponding event is GetResponseForExceptionEvent, which holds the response object (the output of a web request). This is used to catch the exception and modify the response with generic error information instead of showing runtime error to the users.
Advertisements