Finding the Current Module, Controller and Action in Magento

Finding the Current Module, Controller and Action in Magento

Templates and observers often need to know which page they are running on. Magento 1 exposes this through the request object.

The four calls

<?php 

Mage::app()->getRequest()->getControllerName(); // return controller name
 
Mage::app()->getRequest()->getActionName(); // return action name
 
Mage::app()->getRequest()->getRouteName(); // return routes name
 
Mage::app()->getRequest()->getModuleName(); // return module name

?>

Example: run code only on category pages

if(Mage::app()->getRequest()->getControllerName() == 'category'){
        /* do something */
}

Combine the values for precise targeting, for example module catalog, controller product and action view for product pages only.

Heads up: this guide applies to Magento 1, which stopped receiving official security updates in June 2020. If your store still runs it, plan a move to Magento 2 or Adobe Commerce soon. We can help with the migration.

Want this done for you? Find out more about our Magento development service.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.