2

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

1

2 Answers 2

2

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/

1
0

Declare this event name(custom_observer_name) in controller execute method

$this->_eventManager->dispatch(
                    'custom_observer_name',
                    [$this->getRequest()->getParams()]
                );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.