---
title: "How to Create Custom Dispatch Event in Magento 2"
url: "https://magecomp.com/blog/create-custom-dispatch-event-magento-2/"
date: "2019-01-15T17:10:40+00:00"
modified: "2025-05-29T05:45:12+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 487
reading_time: "3 min read"
summary: "The coolest thing about Magento is that it supports the Event-driven programming model. The event simply means action and it can be anything like user actions, mouse click, system occurrences or an..."
description: "The coolest thing about Magento 2 is that it supports Event-driven programming model. While developing custom extension, we need to create custom dispatch ev..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Get Shipping Rates by Quote ID in Magento 2?"
    url: "https://magecomp.com/blog/get-shipping-rates-by-quote-id-magento-2/"
  - title: "Laravel 8: Database Seeder with Example"
    url: "https://magecomp.com/blog/laravel-8-database-seeder/"
  - title: "How to fix “Unique constraint violation found” in Magento 2 Checkout Page After Migration"
    url: "https://magecomp.com/blog/fix-unique-constraint-violation-found-magento-2-checkout-after-migration/"
---

# How to Create Custom Dispatch Event in Magento 2

_Published: January 15, 2019_  
_Author: Dhiren Vasoya_  

![How to Create Custom Dispatch Event in Magento 2](https://magecomp.com/blog/wp-content/uploads/2019/01/How-to-Create-Custom-Dispatch-Event-in-Magento-2-1024x332.webp)

The coolest thing about Magento is that it supports the Event-driven programming model. The event simply means action and it can be anything like user actions, mouse click, system occurrences or anything. And the system which responds to events is generally known as an event-driven programming language. The entire concept is pretty straightforward. When an event gets fired, and your observers catch the event and executes a certain piece of code. And the Best part in that, you can write your custom code apart from the core modification.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-1.webp)](https://magecomp.com/services/hyva-theme-development/)
In Magento 2, events can be dispatched using the **Magento\Framework\Event\Manager** class. While developing a custom extension, we need to create a custom dispatch event, so in future, if any other developer needs to extend functionality at that time they can use this dispatch event. So, here it goes.

In the first step, we need to create “**My event.php**” file to perform particular action.
**app\code\vendor\extension\Controller\Index\Myevent.php**

```
namespace VendorExtensionControllerIndex;

class Myevent extends MagentoFrameworkAppActionAction
{
	public function execute()
	{
		$nameobj = new MagentoFrameworkDataObject(array('name' => 'Magecomp'));
		$this->_eventManager->dispatch('magecomp_customevent_update_name', ['obj' => $nameobj]);
		//$this->_eventManager this object has already created in Action Class so don’t need to create again.
		echo $textDisplay->getText();
		return true;
	}
}
```

In next step, we need to create “**events.xml**” file for calling custom dispatch event.
**app\code\vendor\extension\etc\frontend**

```
<?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="magecomp_customevent_update_name">
        <observer name="magecomp_customevent_update_name" instance="VendorExtensionObserverUpdatevalue" />
    </event>
</config>
```

Lastly, Create **“Updatevalue.php”** file inside observer folder of extension.

**app\code\Vendor\Extension\Observer**

```
<?php
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use PsrLogLoggerInterface;

class Updatevalue implements ObserverInterface
{
    protected $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function execute(Observer $observer)
    {
        $object = $observer->getEvent()->getObj();
        $name = $object->getName();
        $this->logger->info('Custom Event Triggered. Name: ' . $name);
    }
}

<a href="https://magecomp.com/services/hire-magento-programmer/" rel="noopener" target="_blank" data-wpel-link="exclude"><img alt="Hire Magento Programmer" class="aligncenter wp-image-37465 size-full not-transparent" data-dominant-color="6c2635" data-has-transparency="false" decoding="async" height="178" loading="lazy" sizes="auto, (max-width: 640px) 100vw, 640px" src="https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp" srcset="https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3.webp 640w, https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3-300x83.webp 300w, https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3-400x111.webp 400w, https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-3-150x42.webp 150w" style="--dominant-color: #6c2635;" width="640"></img></a>
```

In last step, we need to create Event Observer file by creating “**Updatevalue.php**”.
**app\code\vendor\extension\observer\Updatevalue.php**

```
namespace VendorExtensionObserver;

class Updatevalue implements MagentoFrameworkEventObserverInterface
{
	public function execute(MagentoFrameworkEventObserver $observer)
	{
		$displayname = $observer->getData('obj');
		echo $displayname->getName() . " - Event ";
		$displayname->setText('Execute event successfully.');

		return $this;
	}
}
```

That’s it! You have successfully created your custom dispatch event in Magento 2. You are free to play and manipulate this according to your need for creating custom dispatch event in Magento 2 Extension.
That’s it for today, Let us know if you are facing an issue while implementing using this code by commenting below.

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)
Happy Coding!


---

_View the original post at: [https://magecomp.com/blog/create-custom-dispatch-event-magento-2/](https://magecomp.com/blog/create-custom-dispatch-event-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-25 10:54:45 UTC_  
