Get Current URL from a PHTML - Magento 2
It's working for me
$this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true])
Also instead $this you can use $block.
To do so you will need to use a custom block from a module and do the following:
In Magento 2, you can use the \Magento\Store\Model\StoreManagerInterface
which is stored in an accessible variable $_storeManager
for every class extending \Magento\Framework\View\Element\Template
so most of the block classes (Template
, Messages
, Redirect
block types but not Text
nor TextList
).
This way in your block, you can create the following method to get the current URL:
public function getCurrentUrl() {
return $this->_storeManager->getStore()->getCurrentUrl();
}
Once you've done that you can call the following in your template:
$block->getCurrentUrl();
$block->getRequest()->getUriString();