Global Event Manager, how to setup?


#1

https://book.cakephp.org/3.0/en/core-libraries/events.html#global-event-manager

What is $aCallback variable in:

// In any configuration file or piece of code that executes before the event
use Cake\Event\EventManager;

EventManager::instance()->on(
    'Model.Order.afterPlace',
    $aCallback
); 

??

How to create Event class in plugin, and then use?


#2

dump, how to understand cakephp events if documentation of implementations it´s not clear?


#3

what do you mean ‘not clear’?

https://book.cakephp.org/3.0/en/core-libraries/events.html#registering-listeners

EvOne.php // yeah im lazy
use Cake\Event\EventListenerInterface;

class EvOne implements EventListenerInterface
{
    public function implementedEvents()
    {
        return [
            'Model.Order.someEvent' => 'updateBuyStatistic',
        ];
    }

    public function updateBuyStatistic($event)
    {
        dd(1);
        // Code to update statistics
    }
}

https://book.cakephp.org/3.0/en/core-libraries/events.html#global-event-manager

// In any configuration file or piece of code that executes before the event

so anywhere in code just before dispatching event, like in AppController::beforeFilter

EventManager::instance()->on(new EvOne());

https://book.cakephp.org/3.0/en/core-libraries/events.html#dispatching-events

EventManager::instance()->dispatch('Model.Order.someEvent');

#4

this part in book: ‘Model.Order.afterPlace’, $aCallback

// In any configuration file or piece of code that executes before the event
use Cake\Event\EventManager;

EventManager::instance()->on(
    'Model.Order.afterPlace',
    $aCallback
);

#5

callback is callback:

$aCallback = function() {
            dd(2);
        };
        EventManager::instance()->on('Model.Order.someEvent', $aCallback);

// or

EventManager::instance()->on('Model.Order.someEvent', function() {
            dd(2);
        });