How to Get Controller of Each Request in Magento 2

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!

Previous Article

MageComp Santastic Christmas Deals 2021: Flat 15% OFF on all Magento 2 Extensions

Next Article

8 Social Media Strategies to Drive Traffic to eCommerce Site

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨