Magento 2: Get Current URL and Base URL in phtml

Magento 2: Get Current URL and Base URL in phtml

As a Magento developer, you must be adept in getting current and base URLs in phtml. This can prove beneficial when customising a theme or simply enhancing the store’s functionality. However, even the best Magento development experts cannot recall how to get current URL in Magento 2 or the base URL. There’s nothing to worry about since today’s discussion will let you know everything there is to know about using phtml to get current and base URL in Magento 2.

Current and Base URLs – Understanding the Difference

Before we address how to get base URL in Magento 2 or the current URL, let’s understand the difference between the two.

The base URL is the root URL of any website. For instance, whenever you open a link on Facebook, the URL ‘www.facebook.com’ will always appear at the start. This is the base URL. In the context of eCommerce, the base URL is the root URL if your online store. In our case, it will be ‘www.fmeextensions.com’.

Read More:

On the other hand, the current URL is the page URL that is currently open. For example, we are currently browsing our Multi Select Layered Navigation extension. This Magento 2 extension allows store owners to make category navigation fast and user-friendly. The current URL is:

As you can see, ‘www.fmeextensions.com’ appears at the start. Let us now shift our focus towards getting current and base URLs using phtml.

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>';

Conclusion

This concludes our article on how to get current and base URL in Magento 2 in phtml. If you have any Magento related queries, consult our expert Magento consultants. They will guide you everything there is to know about ‘Magento 2 get base URL in phtml’ and ‘Magento 2 get current URL’.