PHPackages                             ikkez/f3-events - 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. ikkez/f3-events

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

ikkez/f3-events
===============

Sweet event system for the PHP Fat-Free Framework

v1.0.3(4y ago)2822.1k↓20.8%5[1 issues](https://github.com/ikkez/f3-events/issues)3GPL-3.0PHP

Since Mar 27Pushed 4y ago6 watchersCompare

[ Source](https://github.com/ikkez/f3-events)[ Packagist](https://packagist.org/packages/ikkez/f3-events)[ Docs](https://github.com/ikkez/f3-events)[ RSS](/packages/ikkez-f3-events/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (3)

Sugar Events
============

[](#sugar-events)

This is a event system for the PHP Fat-free Framework. Here is what's included so far:

- emit events from any point of your app
- attach one or multiple listeners to an event
- a listener (hook) can have a priority order
- additional options for listeners
- local events on specific objects
- send payload and context data with an event
- sub-events and event propagation
- stop the event chain
- works with F3 v3.5 and PHP v7.2+

---

This event system is **experimental**, so please handle with care.

### Installation

[](#installation)

- Method 1: use composer: `composer require ikkez/f3-events`
- Method 2: copy the `lib/event.php` file into your F3 `lib/` directory or another directory that is known to the [AUTOLOADER](https://fatfreeframework.com/quick-reference#AUTOLOAD)

### How does it work:

[](#how-does-it-work)

The Event class is a child of Prefab, so you can get it everywhere like this:

```
// fetch the global Event instance
$events = \Sugar\Event::instance();
```

Define a listener / hook:

```
// typical F3 callstring
$events->on('user_login', 'Notification->user_login');
// or with callbacks
$events->on('user_login', function(){
  // ...
});
// or with callables
$events->on('user_login', [$this,'method']);
```

Send an event:

```
$events->emit('user_login');
```

Send payload with event:

```
$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
});
$events->emit('user_login', 'freakazoid');
```

Multiple listeners with prioritization:

```
$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
}, 10); // 10 is default priority
$events->on('user_login', function(){
  \Flash::addMessage('You have logged in successfully');
}, 20); // 20 is a higher priority and is called first
$events->emit('user_login', 'freakazoid');
```

Stop the event chain:

```
$events->on('user_login', function($username){
  \Logger::log($username.' logged in');
});
$events->on('user_login', function(){
  \Flash::addMessage('You have logged in successfully');
  return false; // emit('user_login', 'freakazoid');
// The logger event isn't called anymore
```

Additional event context data:

```
$events->on('user_login', function($username,$context){
  if ($context['lang'] == 'en')
    \Flash::addMessage('You have logged in successfully');
  elseif($context['lang'] == 'de')
    \Flash::addMessage('Du hast dich erfolgreich angemeldet');
});
$events->emit('user_login', 'freakazoid', array('lang'=>'en'));
```

Additional listener options:

```
$events->on('user_login', function($username,$context,$event){
  \Flash::addMessage('You have logged in successfully', $event['options']['type']);
}, 20, array('type'=>'success'));
```

I think that are the basic usage samples that could fit the most cases. Nevertheless here are some more advanced things you can do:

Filter payload:

```
$events->on('get_total', function($basket){
  $sum = 0;
  foreach($basket as $prod) {
    $sum+=$prod;
  }
  return $sum;
});

$products = array(
  'a' => 2,
  'b' => 8,
  'c' => 15,
);

$sum = $events->emit('get_total',$products);
echo $sum; // 25
```

Add a sub-event. These are called after the parent event. Listeners and sub-events follow the FIFO processing, which means the first that is registered is the first that will be called.

```
$events->on('get_total.tax', function($sum){
  return $sum+($sum*0.2);
});
$events->on('get_total.shipping', function($sum){
  return $sum+5;
});
$sum = $events->emit('get_total',$products);
echo $sum; // 35
```

Remove hooks:

```
$events->off('get_total.tax');
$sum = $events->emit('get_total',$products);
echo $sum; // 30
```

There is also a mechanic build in which supports local events for mappers and such, which have implemented it:

```
$user = new \Model\User();
$events->watch($user)->on('update.email','\Mailer->sendEmailActivationLink');
```

### Unit tests

[](#unit-tests)

to add the tests to your local F3 test-bench, add this:

```
// Event Tests
$f3->concat('AUTOLOAD', ',path/to/f3-events/test/');
\Sugar\EventTests::init();
```

License
-------

[](#license)

You are allowed to use this plugin under the terms of the GNU General Public License version 3 or later.

Copyright (C) 2017 Christian Knuth \[ikkez\]

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~561 days

Total

4

Last Release

1654d ago

Major Versions

v0.9 → v1.0.12020-01-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/84cac52f5889d2bf6e710f2139dfc884824b2d7ce9c48a3bfe90704a94c85722?d=identicon)[ikkez](/maintainers/ikkez)

---

Top Contributors

[![ikkez](https://avatars.githubusercontent.com/u/1177647?v=4)](https://github.com/ikkez "ikkez (8 commits)")

---

Tags

eventsfat-free-frameworkhookseventF3fatfree

### Embed Badge

![Health badge](/badges/ikkez-f3-events/health.svg)

```
[![Health](https://phpackages.com/badges/ikkez-f3-events/health.svg)](https://phpackages.com/packages/ikkez-f3-events)
```

###  Alternatives

[doctrine/event-manager

The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.

6.1k501.1M115](/packages/doctrine-event-manager)[league/event

Event package

1.6k141.6M184](/packages/league-event)[laminas/laminas-eventmanager

Trigger and listen to events within a PHP application

1.0k69.8M225](/packages/laminas-laminas-eventmanager)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52113.7M18](/packages/winzou-state-machine)[spatie/laravel-event-sourcing

The easiest way to get started with event sourcing in Laravel

9003.7M26](/packages/spatie-laravel-event-sourcing)[spatie/laravel-google-calendar

Manage events on a Google Calendar

1.4k1.5M21](/packages/spatie-laravel-google-calendar)

PHPackages © 2026

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