Get Controller, Module, Action & Router Name In Magento



In Magento sometimes we can not get the actual name of the Controller, Module, Action & Router, Magento has many numbers of the Controller, Module, Action & Router, so it is very difficult to find the actual name of the Controller, Module, Action & Router. Here in this post I am trying to describe how you can get the name of the Controller, Module, Action & Router easily in template file or in any class file?.

Finding Controller, Module, Action & Router Name In Template (phtml) Files:

1. Get Controller Name In Template Files:

$this->getRequest()->getControllerName();

2. Get Action Name In Template Files:

$this->getRequest()->getActionName(); //the function inside the controller

3. Get Router Name In Template Files:

$this->getRequest()->getRouteName();

4. Get Module Name In Template Files:

$this->getRequest()->getModuleName();

Finding Controller, Module, Action & Router Name In Class Files:

5. Get Controller Name In Class Files:

Mage::app()->getRequest()->getControllerName();

6. Get Action Name In Class Files:

Mage::app()->getRequest()->getActionName();

7. Get Router Name In Class Files:

Mage::app()->getRequest()->getRouteName();

8. Get Module Name In Class Files:

Mage::app()->getRequest()->getModuleName();

These functions i.e. getControllerName, getActionName, getRouteName, getModuleName are present in the class Mage_Core_Model_Url, you can display all the results by using the print_r();

like as

echo "<pre>";
    print_r(Mage::app()->getRequest());
echo "</pre>";

This is all about How we can get Controller, Module, Action & Router Name In Magento, Hope this would be helpful, Thanks and Enjoy the coding.