Magento2 REST API – How to Dispatch Events

event-observermagento2rest api

I have custom module and my code structure is as per service contact.

app/code/[Vendor]/[Module]/Model/AccountManagement.php

Now i have a dispatch event in my model file

$this->_eventManager->dispatch('vendor_register_success', $eventParams);

As per REST API standard and as per the this solution i added event in specific module where it comes from.

app/code/[Vendor]/[Module]/etc/webapi_rest/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="vendor_register_success">
        <observer name="vendor_register_success" instance="[Vendor]\[Module]\Observer\VendorBrandRegister" />
    </event>   
</config>

and My observer looks like this

app/code/[Vendor]/[Module]/Observer/VendorBrandRegister.php

 public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "In execute"; exit;
    }

but this event is not dispatching.
What am i missing Because as per the solution i need to add events in webapi_rest folder in etc folder.

Best Answer

Try below code for dispatch event.

$this->_eventManager->dispatch('vendor_register_success', [$eventParams]);

Instead of

$this->_eventManager->dispatch('vendor_register_success', $eventParams);

As second parameter should be an array. This may help you.

Related Topic