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


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

    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
        $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/>";

  • hi @Manashvi,can we get controller and action name from referralUrl? – jafar pinjar Dec 11 '18 at 10:00

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(); 
  • 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

You can also do :


To get the full action name


You can get these information from request object.


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.