27

Magento 2: How to get controller, module, action and router name?

| improve this question | | | | |
39

Use the below code in the controller class to get the controller, module, action and route name:

<?php
    namespace Custom\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $request;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Request\Http $request
    ){
        parent::__construct($context);
        $this->request = $request;
    }

    public function execute()
    {
        $moduleName = $this->request->getModuleName();
        $controller = $this->request->getControllerName();
        $action     = $this->request->getActionName();
        $route      = $this->request->getRouteName();

        echo $moduleName."<br/>";
        echo $controller."<br/>";
        echo $action."<br/>";
        echo $route."<br/>";

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}
| improve this answer | | | | |
  • hi @Manashvi,can we get controller and action name from referralUrl? – jafar pinjar Dec 11 '18 at 10:00
15

to get phtml file or controller use below

echo $controllerName = $this->getRequest()->getControllerName();
echo $actionName = $this->getRequest()->getActionName();
echo $routeName = $this->getRequest()->getRouteName();
echo $moduleName = $this->getRequest()->getModuleName(); 
| improve this answer | | | | |
  • How can I get home page controller action to set an observer ? – supriya mishra Jan 27 '17 at 6:42
  • if you test this code it will output for homepage controller:index,action:index,route:cms,module:cms hope this will help. – Qaisar Satti Jan 27 '17 at 7:25
  • @QaisarSatti, can we get controller and action name from the referral url? $this->redirect->getRefererUrl(); – jafar pinjar Dec 11 '18 at 9:29
6

Use below code snippets to phtml,controller and events in magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface');

$routeName      = $requestInterface->getRouteName();
$moduleName     = $requestInterface->getModuleName(); 
$controllerName = $requestInterface->getControllerName(); 
$actionName     = $requestInterface->getActionName();
| improve this answer | | | | |
  • 3
    You should not instantiate the ObjectManager directly. You should inject the needed classes/objects through DI. – 7ochem May 17 '16 at 12:49
6

You can also do :

$this->_requestInterface->getFullActionName()

To get the full action name

| improve this answer | | | | |
1

You can get these information from request object.

Example

In your controller class:

$routeName        = $this->getRequest()->getRouteName();
$moduleName       = $this->getRequest()->getModuleName();
$controllerName   = $this->getRequest()->getControllerName();
$actionName       = $this->getRequest()->getActionName();

I hope this will help.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.