PHPackages                             tweekersnut/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. tweekersnut/event-dispatcher

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tweekersnut/event-dispatcher
============================

Provides tools that allow your application components to communicate with each other by dispatching events and listening to them. Supports both Laravel and core PHP.

V1.0.0(6mo ago)04MITPHPPHP ^7.4|^8.0

Since Oct 22Pushed 3mo agoCompare

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

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Event Dispatcher
================

[](#event-dispatcher)

A flexible and powerful event dispatcher package for PHP that allows your application components to communicate with each other by dispatching events and listening to them. Supports both **Laravel** and **core PHP**.

[![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT)

Features
--------

[](#features)

- 🚀 **Simple and intuitive API**
- 🎯 **Priority-based event listeners**
- 📦 **Event subscribers for grouping related listeners**
- 🔄 **Event propagation control**
- 🎨 **Works with both Laravel and core PHP**
- 💪 **Type-safe with interfaces and contracts**
- 🧪 **Easy to test**
- 📝 **Well documented with examples**

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

[](#installation)

### For Laravel Projects

[](#for-laravel-projects)

Install the package via Composer:

```
composer require tweekersnut/event-dispatcher
```

The service provider will be automatically registered via Laravel's package discovery.

### For Core PHP Projects

[](#for-core-php-projects)

1. Install the package via Composer:

```
composer require tweekersnut/event-dispatcher
```

2. Include the Composer autoloader in your project:

```
require_once __DIR__ . '/vendor/autoload.php';
```

### For Development/Testing

[](#for-developmenttesting)

Clone the repository and install dependencies:

```
git clone https://github.com/TaranpreetSinghRayat/event-dispatcher.git
cd event-dispatcher
composer install
```

Run the examples:

```
php examples/core-php/basic-usage.php
```

Or use the interactive example runner:

```
php run-examples.php
```

Quick Start
-----------

[](#quick-start)

### Core PHP

[](#core-php)

```
use EventDispatcher\EventDispatcher;
use EventDispatcher\Event;

// Create dispatcher instance
$dispatcher = new EventDispatcher();

// Listen to an event
$dispatcher->listen('user.created', function ($event) {
    echo "User created: " . $event->getData('name');
});

// Dispatch an event
$event = new Event('user.created', ['name' => 'John Doe']);
$dispatcher->dispatch($event);
```

### Laravel

[](#laravel)

The service provider is automatically registered via Laravel's package discovery.

```
use EventDispatcher\Laravel\Facades\EventDispatcher;
use EventDispatcher\Event;

// Listen to an event
EventDispatcher::listen('user.created', function ($event) {
    Log::info('User created: ' . $event->getData('name'));
});

// Dispatch an event
$event = new Event('user.created', ['name' => 'John Doe']);
EventDispatcher::dispatch($event);
```

Getting Started
---------------

[](#getting-started)

### Step 1: Installation

[](#step-1-installation)

**For Laravel:**

```
composer require tweekersnut/event-dispatcher
```

**For Core PHP:**

```
composer require tweekersnut/event-dispatcher
```

### Step 2: Try the Examples

[](#step-2-try-the-examples)

After installation, you can run the included examples to see the package in action:

```
# Run basic usage example
php vendor/tweekersnut/event-dispatcher/examples/core-php/basic-usage.php

# Or if you cloned the repository
php examples/core-php/basic-usage.php
```

### Step 3: Start Using in Your Project

[](#step-3-start-using-in-your-project)

**Core PHP:**

```
