I know about how to use Magento 2 core events. But I want to know how to create custom events and how to override the controller.
advance thanks
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI know about how to use Magento 2 core events. But I want to know how to create custom events and how to override the controller.
advance thanks
Steps for Create custom Event Overserver
Define ur event name in events.xml and Create a class file for same as per below coding.
Vendor/Module/Observer/CustomObserverMethod.php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class CustomObserverMethod implements ObserverInterface
{
/**
* custom event handler
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
//logic herer
}
}
Vendor/Module/etc/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="custom_observer_name">
<observer name="custom_observer_method" instance="Vendor\Module\Observer\CustomObserverMethod"/>
</event>
</config>
Ref Link: https://webkul.com/blog/how-to-create-custom-observers-in-magento2/
Declare this event name(custom_observer_name) in controller execute method
$this->_eventManager->dispatch(
'custom_observer_name',
[$this->getRequest()->getParams()]
);