settermjd/mezzio-eventmanager-integration

This is a small package that simplifies integrating laminas-eventmanager with Mezzio projects built with the Mezzio Skeleton.

0.2.0 2025-05-02 06:20 UTC

This package is auto-updated.

Last update: 2025-05-02 06:24:38 UTC


README

Mezzio / Laminas EventManager Integration

This project provides a simplistic way of integrating laminas-eventmanager with Mezzio projects that are (originally) scaffolded with the Mezzio Skeleton, making it almost trivial to subscribe listeners to events.

Tip

If you're new to Mezzio, check out Mezzio Essentials a practical, hands-on guide, which steps you through how to build an application, from start to finish.

Prerequisites

To use this project, you need the following:

  • Composer installed globally
  • PHP 8.3 or 8.4

Usage

To use the package with an existing Mezzio application, use Composer to add the package as a dependency to your project, as in the following example:

composer require mezzio-eventmanager-integration

Then, to subscribe listeners to events, you need to do two things:

  1. Add a listeners element to the application's configuration, listing the listeners to subscribe to an event, and the priority to subscribe them at. There are two things to be aware of here:
    • If you don't assign a priority, a listener will be subscribed with a default priority of 1.
    • Higher priority values execute earlier. Lower (negative) priority values execute later.
  2. The listeners must be registered as services in the DI container.

There are many ways to add a listeners element to the application's configuration, but likely the simplest is to create a new file in the config/autoload directory named listeners.global.php, and in that file, add a configuration similar to the example below.

<?php

return [
    'listeners' => [
        FakeLoggerListener::class       => [
            'event'    => 'test-event',
            'priority' => 10,
        ],
        FakeNotificationListener::class => [
            'event'    => 'test-event',
            'priority' => 20,
        ],
    ],
];

return [
    'listeners' => [
        'add-item' => [
            [
                'listener' => FakeLoggerListener::class,
                'priority' => 10,
            ],
            [
                'listener' => FakeNotificationListener::class,
                'priority' => 10,
            ]
        ],
        'update-item' => [
           [
                'listener' => FakeLoggerListener::class,
           ]
        ],
        'delete-item' => [
            [
                'listener' => FakeLoggerListener::class,
                'priority' => 10,
            ],
        ],
    ],
];

Assuming the configuration above, FakeLoggerListener and FakeNotificationListener are now listening for (or subscribed to) the "test-event" event. When the event is triggered, FakeLoggerListener will execute first, then FakeNotificationListener will execute (assuming that execution isn't short-circuited).

Contributing

If you want to contribute to the project, whether you have found issues with it or just want to improve it, here's how:

  • Issues: ask questions and submit your feature requests, bug reports, etc
  • Pull requests: send your improvements

Did You Find The Project Useful?

If the project was useful, and you want to say thank you and/or support its active development, here's how:

  • Add a GitHub Star to the project
  • Write an interesting article about the project wherever you blog

Disclaimer

No warranty expressed or implied. Software is as is.