How To

How to Get Controller of Each Request in Magento 2

Hello Magento Friends,

In today’s Magento 2 Tutorial Blog, I am going to justify How to Get Controller of Each Request in Magento 2.

Controller in Magento 2 is a file located at a specific place and it responds to a specific URL. If you want to get and print the name of each controller that is called at a specific request, then you need to follow the below steps.

Steps to Get Controller of Each Request in Magento 2:

Step 1: Create a file at the below path

app\code\Vendor\Extension\etc\events.xml

Now, add the following code

<?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="custom_observer" instance="Vendor\Extension\Observer\ControllerActionPredispatch" shared="false" />
  </event>
</config>

Step 2: Create a file at the following path

app\code\Vendor\Extension\Observer\MyClass\ControllerActionPredispatch.php

Then, add the below-mentioned code

<?php 

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;

class ControllerActionPredispatch implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
   {
       // insert code here
   }
}

Conclusion:

Accordingly, you can easily Get Controller of Each Request in Magento 2. Get User Data Inside Controller in Magento 2. In case of any difficulty, let me know via the comment section. Share the article with others 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

How to Get Database Value in Shopify Theme App Extension using App Proxy?

In this article, we will learn about how to get database value in the Shopify…

17 hours ago

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

3 days ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

5 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

1 week ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

1 week ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago