Magento 2 get controller module action and router

Today we talk about Magento 2 get controller module action and router.We’ll talk about how to get controller name, module name, action name and router name programmatically in Magento 2.For any suggestions & question, please feel free to drop a comment.

Controller name:
Controller is a class located in module Controller folder. Here is Controller file of module.

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

Module name:

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

Action name:
Action name is located in module Controller file. Here is Action Method controller.

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

Router name:
Router are defined in module router.xml file. Here is Action Method controller.

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

Example:

If you test this code for home page it will give you following output.

Controller:index
Action:index
Route:cms
module:cms

You can use this code in both phtml and controller file.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!

4 thoughts on “Magento 2 get controller module action and router

  1. I have learn some just right stuff here. Certainly worth bookmarking for
    revisiting. I wonder how so much effort you set to make this sort of magnificent informative web site.

  2. O que se passa , a coisa toda vai bem aqui e é claro que cada um está
    compartilhando informações , isso é realmente excelente, continue escrevendo.

  3. I simply want to tell you that I am just beginner to blogging and site-building and certainly enjoyed you’re web page. Very likely I’m planning to bookmark your blog post . You certainly come with perfect posts. Bless you for sharing with us your website.

Leave a Reply