settermjd/mezzio-eventmanager-integration

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

0.2.1 2025-05-06 02:38 UTC

This package is auto-updated.

Last update: 2025-05-08 04:44:03 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

How to subscribe listeners to events

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

  1. Add a listeners element to the application's configuration, listing the events and the listeners that are subscribed to those events, along with the priority to subscribe them at (which is optional). There are three 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' => [
        'add-item' => [
            [
                'listener' => FakeLoggerListener::class,
                'priority' => 10,
            ],
            [
                'listener' => FakeNotificationListener::class,
                'priority' => 20,
            ]
        ],
        'update-item' => [
           [
                'listener' => FakeLoggerListener::class,
           ]
        ],
        'delete-item' => [
            [
                'listener' => FakeNotificationListener::class,
                'priority' => 10,
            ],
        ],
    ],
];

Assuming the configuration above was used:

  • FakeLoggerListener and FakeNotificationListener are subscribed to the "add-item" event
  • FakeLoggerListener is subscribed to the "update-item" with a priority of "1" as a priority was not specified
  • FakeNotificationListener is subscribed to the "delete-item" event

When the "add-item" event is triggered, FakeLoggerListener will execute first as it has a lower priority, 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.

Made with 🖤 in Bundaberg by Matthew Setter.