34

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

5 Answers 5

47

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();
    }
}
1
  • hi @Manashvi,can we get controller and action name from referralUrl? Dec 11, 2018 at 10:00
19

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(); 
3
  • How can I get home page controller action to set an observer ? Jan 27, 2017 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. Jan 27, 2017 at 7:25
  • @QaisarSatti, can we get controller and action name from the referral url? $this->redirect->getRefererUrl(); Dec 11, 2018 at 9:29
7

You can also do :

$this->_requestInterface->getFullActionName()

To get the full action name

2

Try not to use Objectmanager. It is just for fast implementation

If the url is http://localhost/mage242/customer/account/login

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

Vote this answer up if it helps.

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.

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.