How to get base url, current url in Magento 2

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.

To get base url and current url in Magento 2:

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Get current URL and base URL in the template (.phtml) file
  • Step 3: Flush Cache and check result

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. If that you have any queries about the article or any questions in general, use the comment section below!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

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

Comments for How to get base url, current url in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

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