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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.