How to Get Magento 2 Base URL & Current URL in phtml

Magento 2, one of the most robust eCommerce platforms, provides a variety of ways to retrieve vital information for development. Two commonly requested pieces of data during Magento 2 development are the base URL and the current URL. Whether you’re working on themes, modules, or custom integrations, understanding how to access these URLs efficiently can streamline your development process and enhance the user experience.

In this article, we’ll guide you step by step on how to get the base URL and current URL in Magento 2.

3 Steps to Get Base URL and Current URL in Magento 2

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of StoreManagerInterface and UrlInterface in the constructor of the module’s block class. You will work with two functions in the below class: getStoreManagerData() and getUrlInterfaceData().

  • In getStoreManagerData() function, you will use object of StoreManagerInterface to get the base and current URL.

  • In getUrlInterfaceData() function, you will use object of UrlInterface to get the base and current URL.

Open app/code/Mageplaza/HelloWorld/Block/HelloWorld.php class and run the code:

<?php
namespace Mageplaza\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('helloworld/general/enabled') . '<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

Step 2: Get base URL and current URL in the template file

Please use the following code:

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

Step 3: Flush Cache and check result

Flush Magento cache and check your result.

Conclusion

That’s all. Getting based and current URLs simplifies the work for store admins as they do not need to find and type in the full URL when building a page or attaching the URLs somewhere. Hope that you will find this article useful for your project.

Read also: How to Configure Store URLs in Magento 2

Related Post

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 get base url
  • current url
  • magento 2 how to get base url
  • current url
  • get base url magento 2
  • get current url in magento 2
  • get base url in magento 2
  • magento 2 get url
  • magento 2 get base url in phtml
  • get base url magento 2
  • get url magento 2
  • how to get base url in magento 2
  • magento 2 get current url
  • magento 2 get store url
  • base url magento 2
  • get store url in magento 2
  • get url in magento 2
  • magento 2 base url in phtml
  • get base url in magento2
  • magento 2 get base url in controller
  • getbaseurl magento 2
  • magento 2 get url in phtml
  • get current url in magento 2
  • geturl magento 2
  • magento 2 get base url in phtml file
  • magento 2 get base url in html file
  • get current url magento 2
  • magento 2 phtml get base url
  • magento 2 get current url in phtml
  • 2.3.x, 2.4.x
x