CakePHP event dispatcher library that helps implementing the observer pattern

5.0.7 2024-01-05 16:56 UTC

This package is auto-updated.

Last update: 2024-04-06 02:38:51 UTC


Total Downloads License

CakePHP Event Library

This library emulates several aspects of how events are triggered and managed in popular JavaScript libraries such as jQuery: An event object is dispatched to all listeners. The event object holds information about the event, and provides the ability to stop event propagation at any point. Listeners can register themselves or can delegate this task to other objects and have the chance to alter the state and the event itself for the rest of the callbacks.


Listeners need to be registered into a manager and events can then be triggered so that listeners can be informed of the action.

use Cake\Event\Event;
use Cake\Event\EventDispatcherTrait;

class Orders

	use EventDispatcherTrait;

	public function placeOrder($order)
		$event = new Event('Orders.afterPlace', $this, [
			'order' => $order

$orders = new Orders();
$orders->getEventManager()->on(function ($event) {
	// Do something after the order was placed
}, 'Orders.afterPlace');


The above code allows you to easily notify the other parts of the application that an order has been created. You can then do tasks like send email notifications, update stock, log relevant statistics and other tasks in separate objects that focus on those concerns.


Please make sure you check the official documentation