SlideShare une entreprise Scribd logo
1  sur  11
ZF2 Event Manager
by Bo Andersen
Introduction to the event system
ZF2 is based on the Event-driven Architecture (EDA)
Simply put, the idea is that activities occur in response to events
Thus, activities are attached to events, and when a given event occurs, its
attached activities are executed
This principle is well known in programming; Java and the .NET platform, for
instance, both make heavy use of event handling
Configuring ZF2 with events
Events are used internally in Zend Framework 2, e.g. by the module manager
Subscribing to these events allows one to “plug into” the framework
A series of pre-defined events are available
Please see the documentation for more information and a full list of available events
Example use case
Imagine placing an order on Amazon.com
When the order is placed, a series of activities or processes should occur
A confirmation should be sent to the customer
The items should be picked from the warehouse
The customer’s credit card should be debited
...
These processes are registered on the OnOrderPlaced event, and when this
event is triggered, the attached processes or activities are executed
Advantages
Provides a great deal of flexibility in a system
Easy to add or remove activities that should happen when a given event occurs
The application becomes very flexible, as it is easy to “hook into” the system
Business processes can easily be adapted to changing business needs
Disadvantages
It can be hard to understand what actually happens during a business process
Activities can be attached to events almost anywhere in the application, and
perhaps even added dynamically
This makes it difficult to both understand and debug a process; one must know which activities are
attached to an event at runtime to understand exactly what happens
The flow of the business process is therefore less transparent than if it were implemented in a
completely procedural fashion
The sequence in which activities must be executed adds complexity
The Event Manager
Example: Triggering an event
class UserService implements EventManagerAwareInterface {
public function setEventManager(EventManagerInterface $events) { /* ... */ }
public function getEventManager() { /* ... */ }
public function register(User $user) {
$this->getEventManager()->trigger(__function__ . '.pre', $this, array(‘user’
=> $user));
// Register code here
$this->getEventManager()->trigger(__function__ . '.post', $this, array(‘user’
=> $user));
}
}
Example: Attaching event listener
$userService = new UserService();
$userService->setEventManager(new EventManager());
$userService->getEventManager()->attach('register.post', function(EventInterface $event) {
$logger = new ZendLogLogger();
$logger->addWriter(new ZendLogWriterStream('php://output'));
$logger->log(ZendLogLogger::INFO, 'New user (ID: ' . $event->getParam('user')->getId() .
')');
});
More information
A priority can also be specified when attaching an event listener to an event
The priority is simply an integer; the listener with the highest number is executed first
A callback can be assigned to multiple events at once
A wildcard can also be used to attach a callback to all events
More information and examples: http://tinyurl.com/zf2-event-manager
that’s allThis presentation is part of my Zend Framework 2 online course.
Get 50% discount by navigating to the below URL!
https://www.udemy.com/zend-framework-2-from-beginner-to-
professional/?couponCode=SS50

Contenu connexe

En vedette

Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
massimiliano.wosz
 

En vedette (11)

Into the ZF2 Service Manager
Into the ZF2 Service ManagerInto the ZF2 Service Manager
Into the ZF2 Service Manager
 
Zend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency InjectionZend Framework 2 : Dependency Injection
Zend Framework 2 : Dependency Injection
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Zend Framework 2 - Basic Components
Zend Framework 2  - Basic ComponentsZend Framework 2  - Basic Components
Zend Framework 2 - Basic Components
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
Creating fast, dynamic ACLs in Zend Framework (Zend Webinar)
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Zend Framework Workshop Parte2
Zend Framework Workshop Parte2Zend Framework Workshop Parte2
Zend Framework Workshop Parte2
 
GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom GAME ON! Integrating Games and Simulations in the Classroom
GAME ON! Integrating Games and Simulations in the Classroom
 

Plus de Bo Andersen

Plus de Bo Andersen (6)

Elasticsearch Meta Fields
Elasticsearch Meta FieldsElasticsearch Meta Fields
Elasticsearch Meta Fields
 
Introduction to Elasticsearch Types
Introduction to Elasticsearch TypesIntroduction to Elasticsearch Types
Introduction to Elasticsearch Types
 
Introduction to Elasticsearch Searching
Introduction to Elasticsearch SearchingIntroduction to Elasticsearch Searching
Introduction to Elasticsearch Searching
 
Introduction to Elasticsearch Mapping
Introduction to Elasticsearch MappingIntroduction to Elasticsearch Mapping
Introduction to Elasticsearch Mapping
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to Elasticsearch
 
Elasticsearch Field Data Types
Elasticsearch Field Data TypesElasticsearch Field Data Types
Elasticsearch Field Data Types
 

Dernier

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Dernier (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

ZF2 Event Manager

  • 1. ZF2 Event Manager by Bo Andersen
  • 2. Introduction to the event system ZF2 is based on the Event-driven Architecture (EDA) Simply put, the idea is that activities occur in response to events Thus, activities are attached to events, and when a given event occurs, its attached activities are executed This principle is well known in programming; Java and the .NET platform, for instance, both make heavy use of event handling
  • 3. Configuring ZF2 with events Events are used internally in Zend Framework 2, e.g. by the module manager Subscribing to these events allows one to “plug into” the framework A series of pre-defined events are available Please see the documentation for more information and a full list of available events
  • 4. Example use case Imagine placing an order on Amazon.com When the order is placed, a series of activities or processes should occur A confirmation should be sent to the customer The items should be picked from the warehouse The customer’s credit card should be debited ... These processes are registered on the OnOrderPlaced event, and when this event is triggered, the attached processes or activities are executed
  • 5. Advantages Provides a great deal of flexibility in a system Easy to add or remove activities that should happen when a given event occurs The application becomes very flexible, as it is easy to “hook into” the system Business processes can easily be adapted to changing business needs
  • 6. Disadvantages It can be hard to understand what actually happens during a business process Activities can be attached to events almost anywhere in the application, and perhaps even added dynamically This makes it difficult to both understand and debug a process; one must know which activities are attached to an event at runtime to understand exactly what happens The flow of the business process is therefore less transparent than if it were implemented in a completely procedural fashion The sequence in which activities must be executed adds complexity
  • 8. Example: Triggering an event class UserService implements EventManagerAwareInterface { public function setEventManager(EventManagerInterface $events) { /* ... */ } public function getEventManager() { /* ... */ } public function register(User $user) { $this->getEventManager()->trigger(__function__ . '.pre', $this, array(‘user’ => $user)); // Register code here $this->getEventManager()->trigger(__function__ . '.post', $this, array(‘user’ => $user)); } }
  • 9. Example: Attaching event listener $userService = new UserService(); $userService->setEventManager(new EventManager()); $userService->getEventManager()->attach('register.post', function(EventInterface $event) { $logger = new ZendLogLogger(); $logger->addWriter(new ZendLogWriterStream('php://output')); $logger->log(ZendLogLogger::INFO, 'New user (ID: ' . $event->getParam('user')->getId() . ')'); });
  • 10. More information A priority can also be specified when attaching an event listener to an event The priority is simply an integer; the listener with the highest number is executed first A callback can be assigned to multiple events at once A wildcard can also be used to attach a callback to all events More information and examples: http://tinyurl.com/zf2-event-manager
  • 11. that’s allThis presentation is part of my Zend Framework 2 online course. Get 50% discount by navigating to the below URL! https://www.udemy.com/zend-framework-2-from-beginner-to- professional/?couponCode=SS50

Notes de l'éditeur

  1. The event manager can be thought of as an implementation of the observer pattern where the event is the subject and the observers are the attached event listeners.
  2. $this is the target (often be the class from which the event is triggered)