Magento 2 get current url
Magento 2 get current url : While working with magento we often need to know the current url so that we can work with it. Here in this tutorial we are going to explain how to get the current url in magento. We will also explain how to get the current controller name , Action name or Module name.
Magento 2 get current url
You can get the current url in magento as below –
Magento 2 get current url:
echo $currentUrl = $this->getRequest()->getCurrentUrl(); |
The above example will give you the current url. example : http://example.com/test-product
Magento Get Current Controller name
You can get the current controller name in magento as below –
Magento Get Current Controller name:
$controllerName = $this->getRequest()->getControllerName(); |
The above example gives the controller name.
Magento Get Current Action name
You can get the current action name in magento as below –
Magento Get Current Action name :
$actionName = $this->getRequest()->getActionName(); |
The above example gives the action name.
Magento Get Route name
You can get the current route name in magento as below –
Magento Get Current Route name :
$actionName = $this->getRequest()->getRouteName(); |
The above example gives the route name.
Magento Get Module name
You can get the current Module name in magento as below –
Magento Get Current Module name :
$actionName = $this->getRequest()->getModuleName(); |
The above example gives the module name.
Advertisements