---
title: "How to Call Controller when hit any URL from Website in Magento 2?"
url: "https://magecomp.com/blog/call-controller-when-hit-any-url-from-website-magento-2/"
date: "2022-01-21T17:08:18+00:00"
modified: "2025-02-19T05:06:06+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 287
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Controller is liable for specific URL or set of URLs. You can call controller when any URL gets hit from the website in Magento 2."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Create Customer Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/create-customer-programmatically-magento-2/"
  - title: "How to Create a Unique key Column in db_schema.xml file of Magento 2"
    url: "https://magecomp.com/blog/create-unique-key-column-db-schema-xml-file-magento-2/"
  - title: "How to Update Shipping Methods when Street Field Input Change on Checkout Page in Magento 2?"
    url: "https://magecomp.com/blog/update-shipping-methods-when-street-field-change-magento-2/"
---

# How to Call Controller when hit any URL from Website in Magento 2?

_Published: January 21, 2022_  
_Author: Dhiren Vasoya_  

![How to Call Controller when hit any URL from Website in Magento 2](https://magecomp.com/blog/wp-content/uploads/2022/01/How-to-Call-Controller-when-hit-any-URL-from-Website-in-Magento-2-1024x512.webp)

**Hello Magento Friends,**

In today’s blog, I am going to figure out How to Call Controller when hit any URL from the Website in Magento 2?
The controller is a class that is responsible for a specific URL or group of URLs. It receives requests, processes them, and then returns the results. If you have a custom controller with a custom code and you want to call that controller when any URL gets hit from the website. This can be achieved with the help of the below steps.
[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2023/10/Magento-2-Speed-Performance-Optimization-Services-CTA-4.webp)](https://magecomp.com/magento-performance-optimization.html)

## Steps to Call Controller when hit any URL from Website in Magento 2:
**Step 1:** Create **events.xml** at the following location
**app\code\Vendor\Extension\etc\events.xml**

And add the code as mentioned below
```
<?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="controller_action_predispatch">
      <observer name="my_custom_observer" instance="VendorExtensionObserverMyClass" shared="false" />
  </event>
</config>
```

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-2.webp)](https://magecomp.com/services/hyva-theme-development/)

**Step 2:** Now create the **MyClass.php** file in the following location
**app\code\Vendor\Extension\Observer\MyClass.php**

And add the code as follows
```
<?php
namespace VendorExtensionObserver;

use MagentoFrameworkEventObserverInterface;

class MyClass implements ObserverInterface
{
   public function execute(MagentoFrameworkEventObserver $observer)
   {
        // THIS EVENT CALL ON EVERY URL
       // insert your custom code here, according to your requirement
   }
}
```

## Conclusion:
Consequently, this way you can Call Controller when hit any URL from Website in Magento 2. If in case of any trouble, share it with me through the comment box. You can also [**Get Controller of Each Request in Magento 2**](https://magecomp.com/blog/get-controller-of-each-request-magento-2/). Spread the solution with your friends and stay in the know.
[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-1.webp)](https://magecomp.com/services/hire-magento-programmer/)

**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/call-controller-when-hit-any-url-from-website-magento-2/](https://magecomp.com/blog/call-controller-when-hit-any-url-from-website-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-16 16:04:53 UTC_  
