Today we will show how we can get the name of the current route name, module name, controller name, action name, and route name in Magento 2.
You can also check current pages like catalog_category_view, catalog_product_view, etc.
HTTP request use to get this type current name,
Please have a look at the below code and apply according to your requirement.
Using objectmanager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('\Magento\Framework\App\Request\Http'); echo $routeName = $request->getRouteName(); echo $ModuleName = $request->getModuleName(); echo $controllerName = $request->getControllerName(); echo $actionName = $request->getActionName(); echo $fullActionName = $request->getFullActionName(); echo $ControllerModule = $request->getControllerModule();
Using class inject
protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, ) { $this->request = $request; } echo $routeName = $this->request->getRouteName(); echo $ModuleName = $this->request->getModuleName(); echo $controllerName = $this->request->getControllerName(); echo $actionName = $this->request->getActionName(); echo $fullActionName = $this->request->getFullActionName(); echo $ControllerModule = $this->request->getControllerModule();
Related post Check current page action in magento2