Magento 2: How to get Module, Controller, Action & Router Name

While developing a Magento 2 website, many time we might need to get the current module, controller, action or router name. But it is very difficult to find the actual name of the module, controller, action & router. Because Magento has many numbers of module, controller, action & router. In this blog, we will show you how to get the name of the current module, controller, action, and route in Magento 2.

In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the name of the current module, controller, action and route name in Magento 2.

Using Dependency Injection (DI)

Here is the example code to get the name of the current module, controller, action and route name using dependency injection.

In this, we might need to inject the object of \Magento\Framework\App\Request\Http class in the constructor of our module’s block class and access it from the view ( .phtml ) file.

Sample File Path: app/code/YourCompanyName/YourModuleName/Block/YourCustomBlock.php

<?php
namespace YourCompanyName\YourModuleName\Block;

class YourCustomBlock extends \Magento\Framework\View\Element\Template 
{
 
    protected $_request;

    public function __construct( 
        \Magento\Framework\View\Element\Template\Context $context, 
        \Magento\Framework\App\Request\Http $request, 
        array $data = [] 
    ) {
        $this->_request = $request; 
        parent::__construct($context, $data); 
    }

    public function getModuleName() { 
        return $this->_request->getModuleName(); 
    }

    public function getControllerName() { 
        return $this->_request->getControllerName(); 
    }

    public function getActionName() { 
        return $this->_request->getActionName(); 
    }

    public function getRouteName() { 
        return $this->_request->getRouteName(); 
    }

    public function getControllerModule() { 
        return $this->_request->getControllerModule(); 
    }
 
    public function getFullActionName() { 
        return $this->_request->getFullActionName(); 
    }
}

You can find more functions in vendor/magento/framework/App/Request/Http.php.

Now, we can use the functions in our view (.phtml) file as follows.

echo $block->getModuleName() . '<br />';
echo $block->getControllerName() . '<br />';
echo $block->getActionName() . '<br />';
echo $block->getRouteName() . '<br />';
echo $block->getControllerModule() . '<br />';
echo $block->getFullActionName() . '<br />';

Using Object Manager

Here is the example code to get the name of the current module, controller, action and route name using an object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
 
echo $request->getRouteName() . '<br />';
echo $request->getModuleName() . '<br />';
echo $request->getControllerName() . '<br />';
echo $request->getActionName() . '<br />';
echo $request->getFullActionName() . '<br />';
echo $request->getControllerModule() . '<br />';

Hope this helps.

Leave a Reply

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