PHPackages                             silverstripe/event-dispatcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. silverstripe/event-dispatcher

ActiveSilverstripe-vendormodule[Utility &amp; Helpers](/categories/utility)

silverstripe/event-dispatcher
=============================

Publish and subscribe to events in Silverstripe CMS or your Silverstripe application

2.0.0(1y ago)51.3M↓13.1%2[2 issues](https://github.com/silverstripe/silverstripe-event-dispatcher/issues)3BSD-3-ClausePHPPHP ^8.3CI passing

Since Sep 10Pushed 11mo ago9 watchersCompare

[ Source](https://github.com/silverstripe/silverstripe-event-dispatcher)[ Packagist](https://packagist.org/packages/silverstripe/event-dispatcher)[ RSS](/packages/silverstripe-event-dispatcher/feed)WikiDiscussions 1 Synced 1mo ago

READMEChangelog (7)Dependencies (5)Versions (13)Used By (3)

Event dispatcher for Silverstripe CMS
=====================================

[](#event-dispatcher-for-silverstripe-cms)

[![CI](https://github.com/silverstripe/silverstripe-event-dispatcher/actions/workflows/ci.yml/badge.svg)](https://github.com/silverstripe/silverstripe-event-dispatcher/actions/workflows/ci.yml)

This module provides a [PSR-14 event dispatcher](https://www.php-fig.org/psr/psr-14/) to Silverstripe CMS applications. It is useful for [reactive programming](https://en.wikipedia.org/wiki/Reactive_programming)paradigms in lieu of traditional imperative designs through hooks like `onAfterWrite`.

Most of the underlying work is handled by [Symfony's EventDispatcher](https://symfony.com/doc/current/components/event_dispatcher.html), but this can be replaced with through dependency injection.

Requirements
------------

[](#requirements)

- silverstripe/framework: ^4.5

Installation
------------

[](#installation)

`$ composer require silverstripe/event-dispatcher`

Usage
-----

[](#usage)

There are three main components to the API:

- The `Dispatcher` is responsible for registering event listeners and triggering events
- `Listeners` are classes that trigger events, typically via extension hooks
- `Handlers` are classes that execute code in response to an event.

### Dispatching an event

[](#dispatching-an-event)

```
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;

Dispatcher::singleton()->trigger('createOrder', Event::create(
    null,
    [
        'id' => $order->ID,
        'paymentMethod' => $order->PaymentMethod
    ]
));
```

The `trigger` function takes two arguments: `string $eventName` and `EventContextInterface $context`. The `$context`object can pass arbitrary data to any handlers that are subscribed to the event.

### Event handling

[](#event-handling)

Event handlers must implement `EventHandlerInterface`, which requires a `fire()` method.

```
use SilverStripe\EventDispatcher\Dispatch\EventHandlerInterface;
use SilverStripe\EventDispatcher\Event\EventContextInterface;

class OrderHandler implements EventHandlerInterface
{
    public function fire(EventContextInterface $context): void
    {
        $orderID = $context->get('id');
        $paymentMethod = $context->get('paymentMethod');
        // Do something in response to the order being created
    }
}
```

### Subscribing to events

[](#subscribing-to-events)

The best way to register your event handlers with the dispatcher is through config.

```
SilverStripe\Core\Injector\Injector:
  SilverStripe\EventDispatcher\Dispatch\Dispatcher:
    properties:
      handlers:
        # Arbitrary key. Allows other config to override.
        orders:
          on: [ orderCreated ]
          handler: %$MyProject\MyOrderHandler
```

### Unsubscribing to events

[](#unsubscribing-to-events)

To remove an event handler, override the config with an `off` node.

```
SilverStripe\Core\Injector\Injector:
  SilverStripe\EventDispatcher\Dispatch\Dispatcher:
    properties:
      handlers:
        orders:
          off: [ orderCreated ]
```

### Managing events procedurally

[](#managing-events-procedurally)

To interact with the `Dispatcher` instance directly, use a `DispatcherLoaderInterface`.

```
use SilverStripe\EventDispatcher\Dispatch\DispatcherLoaderInterface;
use SilverStripe\EventDispatcher\Dispatch\EventDispatcherInterface;

class MyLoader implements DispatcherLoaderInterface
{
    public function addToDispatcher(EventDispatcherInterface $dispatcher) : void
    {
        $dispatcher->addListener('myEvent', $myHandler);
        $dispatcher->removeListener('anotherEvent', $anotherHandler);
    }
}
```

Then, register the loader in `Injector`.

```
SilverStripe\Core\Injector\Injector:
  SilverStripe\EventDispatcher\Dispatch\Dispatcher:
    properties:
      loaders:
        myLoader: %$MyProject\MyLoader
```

### Action identifiers and context

[](#action-identifiers-and-context)

Each of these handlers is passed a context object that exposes an **action identifier**. This is a string that provides specific information about what happened in the event that the handler can then use in its implementation. For instance, if you want to write event handlers for form submissions, where some handlers are for all form submissions, while others are for specific forms, you might pass the name of the form as an action identifier in your `EventContext`object.

```
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;

Dispatcher::singleton()->trigger('formSubmitted', Event::create(
    'contact',
    [
        'name' => $formData['Name'],
        // etc..
    ]
));
```

Events are always called with `eventName.`. For instance `formSubmitted.contact`, allowing the subscribers to only react to a specific subset of events.

```
SilverStripe\Core\Injector\Injector:
  SilverStripe\EventDispatcher\Dispatch\Dispatcher:
    properties:
      handlers:
        forms:
          # handler for all form submissions
          on: [ formSubmitted ]
          handler: %$MyProject\MyFormHandler
        contactForm:
          # handler for all a specific form
          on: [ formSubmitted.contact ]
          handler: %$MyProject\MyContactHandler
```

In this case, a contact form submission results in two handlers firing, in order of specificity (`formSubmitted.contact`first).

#### How to find your action identifier

[](#how-to-find-your-action-identifier)

The easiest way to debug events is to put breakpoints or logging into the `Dispatcher::trigger()` function. This will provide all the detail you need about what events are triggered when, and with what context.

```
public function trigger(string $event, EventContextInterface $context): void
{
    error_log($event);
    error_log($context->getAction());
    // ...
```

When the logging is in place you just go to the CMS and perform the action you are interested in. This should narrow the list of identifier down to a much smaller subset.

#### Event context

[](#event-context)

In the above example, the contact form data is passed to the `Event` object as context.

```
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;

Dispatcher::singleton()->trigger('formSubmitted', Event::create(
    'contact',
    [
        'name' => $formData['Name'],
        // etc..
    ]
));
```

In a handler, this can be accessed using the `get(string $property)` method.

```
public function fire(EventContextInterface $context): void
{
    $name = $context->get('Name');
    // do more stuff...
}
```

Note that `get` fails gracefully, and will return `null` when a property doesn't exist.

License
-------

[](#license)

See [License](license.md)

Bugtracker
----------

[](#bugtracker)

Bugs are tracked in the issues section of this repository. Before submitting an issue please read over existing issues to ensure yours is unique.

If the issue does look like a new bug:

- Create a new issue
- Describe the steps required to reproduce your issue, and the expected outcome. Unit tests, screenshots and screencasts can help here.
- Describe your environment as detailed as possible: Silverstripe version, Browser, PHP version, Operating System, any installed Silverstripe modules.

Please report security issues to the module maintainers directly. Please don't file security issues in the bugtracker.

Development and contribution
----------------------------

[](#development-and-contribution)

If you would like to make contributions to the module please ensure you raise a pull request and discuss with the module maintainers.

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity76

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~141 days

Recently: every ~44 days

Total

12

Last Release

525d ago

Major Versions

0.1.3 → 1.0.02022-09-06

1.0.x-dev → 2.0.02024-12-09

PHP version history (4 changes)0.1.0PHP ^7.2

0.1.3PHP ^7.3 || ^8.0

1.0.0PHP ^8.1

2.0.0PHP ^8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/654636?v=4)[Aaron Carlino](/maintainers/unclecheese)[@unclecheese](https://github.com/unclecheese)

![](https://www.gravatar.com/avatar/b0cba8b534e20e6ab4fff555a97b237a18436ebca1446fc0b29c8a8b504038b9?d=identicon)[GuySartorelli](/maintainers/GuySartorelli)

---

Top Contributors

[![emteknetnz](https://avatars.githubusercontent.com/u/4809037?v=4)](https://github.com/emteknetnz "emteknetnz (4 commits)")[![GuySartorelli](https://avatars.githubusercontent.com/u/36352093?v=4)](https://github.com/GuySartorelli "GuySartorelli (4 commits)")[![chillu](https://avatars.githubusercontent.com/u/111025?v=4)](https://github.com/chillu "chillu (3 commits)")[![robbieaverill](https://avatars.githubusercontent.com/u/5170590?v=4)](https://github.com/robbieaverill "robbieaverill (1 commits)")

---

Tags

hacktoberfesteventssilverstripecms

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/silverstripe-event-dispatcher/health.svg)

```
[![Health](https://phpackages.com/badges/silverstripe-event-dispatcher/health.svg)](https://phpackages.com/packages/silverstripe-event-dispatcher)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
