How to generate an event in Magento 2

Here I will show you how to generate an event in your extension for Magento 2

  • This guide is very easy to do, so follow these simple steps
  • The following examples:
<?php

..

$event_data_array  =  ['cid' => '123'];

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$eventManager = $objectManager->create(‘\Magento\Framework\Event\Manager’);

$this->_eventManager->dispatch('my_custom_event', $event_data_array);

..

?>

As you can observe it’s quite simple to dispatch the event from almost anywhere in Magento. The first argument of the sending method is the name of the event and the second argument is used to send data to viewers.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *