Events and observers

Magento implements the observer pattern through \Magento\Framework\Event\ManagerInterface. In app/etc/di.xml, there is a preference for ManagerInterface that points to the Magento\Framework\Event\Manager\Proxy class type. The Proxy class further extends the \Magento\Framework\Event\Manager class that implements the actual event dispatch method.

Events are dispatched by calling a dispatch method on the instance of the Event\Manager class and passing the name and some data, which is optional, to it. Here's an example of a Magento core event:

$this->eventManager->dispatch(
    'customer_customer_authenticated',
    ['model' => $this->getFullCustomerObject($customer), 'password' => $password]
);

The $this->eventManager is an instance of ...

Get Magento 2 Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.