In Magento2 website development, we need to get the current module, controller, action or router name. Magento has many numbers of module, controller, action & router. In this blog, we will show you how to get the name of the current module, controller, action, and route in Magento 2.
Using class
<?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->getRequest()->getModuleName(); $controller = $this->getRequest()->getControllerName(); $action = $this->getRequest()->getActionName(); $route = $this->getRequest()->getRouteName(); echo $moduleName."<br/>"; echo $controller."<br/>"; echo $action."<br/>"; echo $route."<br/>"; $this->_view->loadLayout(); $this->_view->renderLayout(); } } ?>
Using object manager
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('\Magento\Framework\App\Request\Http'); echo $request->getRouteName() . '<br />'; echo $request->getModuleName() . '<br />'; echo $request->getControllerName() . '<br />'; echo $request->getActionName() . '<br />'; echo $request->getFullActionName() . '<br />'; echo $request->getControllerModule() . '<br />'; ?>