Robert 87d5dda5a0 Added dispatchEvent() wrapper method. 11 years ago
..
Event.php 6f3a1c46c7 Merge remote-tracking branch 'origin/2.6' into 3.0 12 years ago
EventListener.php 42757171c5 Merge branch '2.6' into 3.0-merge 11 years ago
EventManager.php 2527d9ebd7 Remove unused use imports 11 years ago
EventManagerTrait.php 87d5dda5a0 Added dispatchEvent() wrapper method. 11 years ago
README.md d83c1e7b4e Fixing Event library readme 11 years ago
composer.json 55cf771de9 Fixing file indentation 11 years ago

README.md

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.

Usage

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\EventManagerTrait;

class Orders {

	use EventManagerTrait;

	public function placeOrder($order) {
		$this->doStuff();
		$event = new Event('Orders.afterPlace', $this, [
			'order' => $order
		]);
		$this->eventManager()->dispatch($event);
	}
}

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

$orders->placeOrder($order);

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.

Documentation

Please make sure you check the official documentation