How to get Magento 2 base URL & Current URL in phtml

How to get base url and current url in Magento 2 is the next topic Mageplaza guides you today. Let's do it by running the code snippet as the following two steps.

When running a Magento 2 store, there will be many situations that need the system to automatically get the base or current URLs, for example, generating the URL for a CMS or a blog page, or developing a development site.

Therefore, How to get base url and current url in Magento 2 is the next topic Mageplaza guides you today. Let’s do it by running the code snippet as the following two steps.

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.

This is the guide to get base url and current url in Magento 2. Getting based and current URL 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. If that you have any queries about the article or any questions in general, use the comment section below!

Read also: How to Configure Store URLs in Magento 2

Related Post

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
how-to
base
url
current
featured

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

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.2.x, 2.3.x, 2.4.x