Magento 2: Get Current URL & Base URL

This article shows how we can get current and base URL in Magento 2.

Both Dependency Injection and Object Manager ways of coding are given below.

Using Dependency Injection (DI)

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of StoreManagerInterface & UrlInterface in the constructor of my module’s block class. Both of them can be used to fetch base and current URL. In below class, in function getStoreManagerData(), object of StoreManagerInterface is used to print the base and current url and in function getUrlInterfaceData() function, object of UrlInterface is used to print the base and current url.

app/code/Chapagain/HelloWorld/Block/HelloWorld.php


<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
        protected $_storeManager;
        protected $_urlInterface;

	public function __construct(
		\Magento\Backend\Block\Template\Context $context,		
		\Magento\Store\Model\StoreManagerInterface $storeManager,
		\Magento\Framework\UrlInterface $urlInterface,	
		array $data = []
	)
	{		
		$this->_storeManager = $storeManager;
		$this->_urlInterface = $urlInterface;
		parent::__construct($context, $data);
	}
	
	public function _prepareLayout()
	{
		return parent::_prepareLayout();
	}
	
	/**
	 * Prining URLs using StoreManagerInterface
	 */
	public function getStoreManagerData()
	{	
		echo $this->_storeManager->getStore()->getId() . '<br />';
		
		// by default: URL_TYPE_LINK is returned
		echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';		
		
		echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . '<br />';
		echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
		echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . '<br />';
		echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC) . '<br />';
		
		echo $this->_storeManager->getStore()->getUrl('product/33') . '<br />';
		
		echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />';
			
		echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />';
			
		echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />';	
	}
	
	/**
	 * Prining URLs using URLInterface
	 */
	public function getUrlInterfaceData()
	{
		echo $this->_urlInterface->getCurrentUrl() . '<br />';
		
		echo $this->_urlInterface->getUrl() . '<br />';
		
		echo $this->_urlInterface->getUrl('test/data/22') . '<br />';
		
		echo $this->_urlInterface->getBaseUrl() . '<br />';
	}
	
}
?>

See more functions in

MAGENTO_ROOT/vendor/magento/module-store/Model/Store.php
MAGENTO_ROOT/vendor/magento/framework/Url.php

As you can see in the above code, my block class is extending class \Magento\Framework\View\Element\Template. Hence, I can easily get URL and base URL in my template (.phtml) file with the following code:


<p>
    <?php echo $block->getUrl('hello/test'); ?>
    <?php echo $block->getBaseUrl(); ?>
</p>

Using Object Manager


$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();		

$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();

echo $store->getUrl('product/33'); echo '<br>';
echo $store->getCurrentUrl(); echo '<br>';
echo $store->getBaseUrl(); echo '<br>';
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); echo '<br>';
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); echo '<br>';

Hope this helps. Thanks.