How To

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

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.

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="Vendor\Extension\Observer\MyClass" shared="false" />
  </event>
</config>

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 Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;

class MyClass implements ObserverInterface
{
   public function execute(\Magento\Framework\Event\Observer $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. Spread the solution with your friends and stay in the know.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

2 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

2 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

3 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

6 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

6 days ago