Magento 2: Get Current URL and Base URL in phtml

Magento 2: Get Current URL and Base URL in phtml
As Magento developer, you have got to deal with different scenarios. They could be either from customer’s side or for your own coding purposes. One such case that you may encounter is that of getting the current and base URL in Magento 2. In the following lines, we have attempted to present a solution. We have tried it and it works. We are confident it will work for you too.

Methods to Get Current URL & Base URL in Magento 2

There are 2 methods to do this.
  • Dependency Injection (DI)
  • Object Manager
Although the 2nd method is not recommended as it consumes extensive resources, it does offer the solution and can be used as an alternative.

1. Using Dependency Injection (DI)

We will use a block class of the custom module FME_HelloWorld. We have injected the object of StoreManagerInterface & UrlInterface in the constructor of the module’s block class. Both of them can be used to fetch base and current URL. We will work with two functions in the below class. GetStoreManagerData() and getUrlInterfaceData().

In function getStoreManagerData(), object of StoreManagerInterface is used to print the base and current URL.

In function getUrlInterfaceData() function, object of UrlInterface is used to get the base and current URL.

Open app/code/FME/HelloWorld/Block/HelloWorld.php.
<?php
namespace FME\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/test2/test3') . '<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

Get Current URL and Base URL in phtml (the template file)

The above block class is extending class \Magento\Framework\View\Element\Template. Hence, we can easily get current URL and base URL in our phtml (template file) with the following code:
 <p>
 <?php echo $block->getUrl('hello/test'); ?>
 <?php echo $block->getBaseUrl(); ?>
</p>

2. Using Object Manager (Not Recommended)

The following code snippet will get base and current URL using object manager in Magento 2.
$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>';
That’s it for this tutorial. If you have any Magento related queries, refer to these best Magento 2 forums or ask our expert Magento consultants. This article is about:
  • magento 2 get base url in phtml
  • magento 2 get current url
  • magento 2 get base url
  • get base url in magento 2

Related Articles: