1

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.

1

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.

| improve this answer | |
  • Tried this solution. Not working. – Aditya Shah Jun 22 '18 at 10:03
  • It's specific webapi_rest related issue.. how to dispatch event in that. – Aditya Shah Jun 22 '18 at 10:04
  • You have done proper, I have seen only difference that I have mentioned. In my opinion First check the code s executed or not? – Prasanta Hatui Jun 22 '18 at 10:34
  • No, it's not going in execute function, now i am not magento allows or not dispatch event in model for service contact – Aditya Shah Jun 22 '18 at 10:39
  • 1
    + for your efforts man !! :) – Aditya Shah Jun 22 '18 at 11:19
2

This can be done by defining events.xml in webapi_rest folder.

vendor/magento/module-quote/etc/webapi_rest/events.xml

Magento is doing it in quote module for sending mail.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="sendEmail" instance="Magento\Quote\Observer\Webapi\SubmitObserver" />
    </event>
</config>

Reference 1 - From Vinai Kopp: https://twitter.com/VinaiKopp/status/810897855527485440

Reference 2

| improve this answer | |

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.