AI Reading
Quick summary of this article
The Zend Framework 3 Event Manager lets different parts of your application communicate through events and listeners, making your code more modular and easier to extend. You can create custom events, attach listeners to respond to them, and use the shared event manager to handle events across different classes—all without changing your core business logic.
- Core components: An event is a signal that something happened (like
user.registered), a listener is a callback that responds to it, and the EventManager triggers events and manages listeners. - Custom events made simple: You can attach a listener to an event with
attach()and trigger it withtrigger(), passing parameters like usernames or other data to the listener. - Integration with classes: By implementing
EventManagerAwareInterface, you can give any class its own EventManager, allowing you to trigger events from methods likeregisterUser(). - Shared Event Manager: This feature lets you attach listeners to events across different classes without modifying them, which is useful for logging, notifications, or caching across your app.
- Best practices: Use events for cross-cutting concerns (like logging or emails), keep listeners lightweight, and avoid overusing events for simple method calls to keep code clear.
Chapter 10: Event Manager and Custom Events in Zend Framework 3
✅ Introduction
The EventManager in Zend Framework 3 allows components to communicate via events and listeners. This makes your application more modular, extensible, and easier to maintain.
✅ Key Concepts
- Event: A signal that something has happened (e.g., user.login).
- Listener: A callback function or method that responds to an event.
- EventManager: The component that triggers events and attaches listeners.
✅ Creating a Custom Event
use Zend\EventManager\EventManager;
$events = new EventManager();
// Attach a listener
$events->attach('user.registered', function($e) {
$params = $e->getParams();
echo "New user registered: " . $params['username'];
});
// Trigger event
$events->trigger('user.registered', null, ['username' => 'JohnDoe']);
✅ Using EventManager in a Class
namespace Application\Service;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
class UserService implements EventManagerAwareInterface {
protected $events;
public function setEventManager(EventManagerInterface $events) {
$this->events = $events;
$this->events->setIdentifiers([__CLASS__, get_class($this)]);
}
public function getEventManager() {
if (!$this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
public function registerUser($username) {
// Business logic for user registration
echo "User $username registered successfully.
";
// Trigger event
$this->getEventManager()->trigger('user.registered', $this, ['username' => $username]);
}
}
✅ Attaching Listeners
$userService = new \Application\Service\UserService();
// Attach listener
$userService->getEventManager()->attach('user.registered', function($e) {
$params = $e->getParams();
echo "Sending welcome email to " . $params['username'];
});
// Trigger event via method
$userService->registerUser('Alice');
✅ Shared Event Manager
The SharedEventManager allows attaching listeners across different classes.
use Zend\EventManager\SharedEventManager;
$sharedEvents = new SharedEventManager();
$sharedEvents->attach(\Application\Service\UserService::class, 'user.registered', function($e) {
echo "Logging registration for " . $e->getParam('username');
});
$userService = new \Application\Service\UserService();
$userService->getEventManager()->setSharedManager($sharedEvents);
$userService->registerUser('Bob');
✅ Best Practices
- Use events for cross-cutting concerns like logging, notifications, and caching.
- Do not overuse events for simple method calls (keeps code clear).
- Keep listeners lightweight; avoid heavy business logic in event callbacks.
✅ Exercise
- Create a
PostServicethat triggers apost.createdevent when a new post is added. - Attach a listener that sends a notification email on post creation.
- Attach another listener that logs the post creation in a file.
🎉 Conclusion
With the EventManager, Zend Framework 3 applications can be extended with custom events and listeners. This promotes modular design and makes it easier to integrate logging, notifications, and external systems without modifying core logic.
