laminas/laminas-eventmanager
Laminas EventManager provides a flexible event and listener system for PHP applications. Attach listeners, trigger events, manage priorities, and use shared event managers to coordinate decoupled components across your app.
Begin by installing the package via Composer:
composer require laminas/laminas-eventmanager
Then, instantiate a basic EventManager (for simple use) or SharedEventManager (for cross-component wiring). The core workflow is:
"user.login"), optionally with a callback or listener object.Event object to encapsulate target, name, and parameters for clarity.Example first use:
$events = new Laminas\EventManager\EventManager();
$events->attach('user.login', function ($e) {
$user = $e->getTarget();
error_log("User {$user->getEmail()} logged in");
});
$events->trigger('user.login', $user);
Check src/EventManager.php, src/SharedEventManager.php, and the examples/ directory (if present) for quick references. The README.md is usually the most up-to-date starting point.
UserEvents::USER_CREATED) to avoid typos and support IDE autocompletion.$priority parameter when attaching listeners (higher = runs earlier); negative values delay execution. Critical listeners (e.g., security checks) often run early ($priority = 100).$sharedEvents->attach(
UserMapper::class,
'fetchById',
[$auditLogger, 'logUserFetch'],
10
);
ListenerAggregateInterface to bundle related listeners (e.g., UserListenerAggregate) and attach/detach them atomically. Ideal for plugins or modules.EventManager (per-object events) with SharedEventManager (system-wide event wiring) to balance granularity and decoupling.false: Returning false from a listener halts subsequent listeners. Use $event->stopPropagation(true) for explicit control.$em->trigger('fetchById', $mapper)), it only fires direct listeners. To trigger SharedEventManager listeners, use SharedEventManager::triggerListeners() in addition to your EventManager, or configure EventManager to delegate to it via setSharedManager().1. If two listeners run in unexpected order, check getPriority() and ensure values are integers (not floats—Laminas casts them, but precision may be lost).ListenerInterface) for long-lived managers.EventManager::getListeners($eventName) to inspect registered handlers. You can wrap this in a dev-only profiler or middleware.Event for structure: Subclass Laminas\EventManager\Event to enforce typed context (e.g., UserLoginEvent extends Event, with getUser(): UserInterface). This prevents getParams() misuse and enables IDE inspection.How can I help you explore Laravel packages today?