Interview frequency: …
Short answer
A Listener handles one specific event, while a Subscriber can subscribe to multiple events and define a handler for each one.
Explanation
Events help build a Decoupled Architecture. A Listener focuses on one event, such as OrderShippedListener. An Event Subscriber groups logic for a domain (such as UserEventSubscriber), handling login, password changes, and logout events in one class through subscribe($events).
Code example
class UserEventSubscriber {
public function handleLogin($event): void {}
public function handleLogout($event): void {}
public function subscribe(Dispatcher $events): array {
return [
Login::class => 'handleLogin',
Logout::class => 'handleLogout',
];
}
}Common mistakes
- Not implementing ShouldQueue on heavy Listeners, delaying the user's HTTP response.