Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laminas Eventmanager Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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:

  1. Attach a listener to an event name (e.g., "user.login"), optionally with a callback or listener object.
  2. Trigger the event on the event manager, passing context (e.g., user data).
  3. Optionally use an 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.

Implementation Patterns

  • Event Abstraction: Define named constants for events (e.g., UserEvents::USER_CREATED) to avoid typos and support IDE autocompletion.
  • Prioritized Listeners: Use the $priority parameter when attaching listeners (higher = runs earlier); negative values delay execution. Critical listeners (e.g., security checks) often run early ($priority = 100).
  • SharedEventManager for Decoupling: In layered apps (e.g., MVC), attach listeners in service factories or providers that depend on core components but shouldn’t modify them. Example:
    $sharedEvents->attach(
        UserMapper::class, 
        'fetchById', 
        [$auditLogger, 'logUserFetch'], 
        10
    );
    
  • Aggregates for Reusability: Implement ListenerAggregateInterface to bundle related listeners (e.g., UserListenerAggregate) and attach/detach them atomically. Ideal for plugins or modules.
  • Hybrid Approach: Combine EventManager (per-object events) with SharedEventManager (system-wide event wiring) to balance granularity and decoupling.

Gotchas and Tips

  • Event propagation stops on false: Returning false from a listener halts subsequent listeners. Use $event->stopPropagation(true) for explicit control.
  • SharedEventManager requires explicit binding: When triggering events on an object (e.g., $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().
  • Listener priority pitfalls: Default priority is 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).
  • Memory leaks with closures: Anonymous functions used in listeners aren’t GC’d automatically if the manager holds a reference. Prefer service objects (e.g., classes implementing ListenerInterface) for long-lived managers.
  • Debugging tip: Use EventManager::getListeners($eventName) to inspect registered handlers. You can wrap this in a dev-only profiler or middleware.
  • Extend 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4