How to Get Base URL, Store URL and Current URL for Magento 2

Using Dependency Injection

Here is the example code to get the store URLls in Magento 2 using dependency injection.

In this, we might need to inject the object of \Magento\Store\Model\StoreManagerInterface class in the constructor of our module’s class.

<?php
namespace YourCompanyName\YourModuleName\Block;
 
class YourCustomBlock extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;
 
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }
 
    public function getBaseUrl() {
        return $this->_storeManager->getStore()->getBaseUrl();
    }
 
    public function getLinkUrl() {
        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);
    }
 
    public function getMediaUrl() {
        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    }
 
    public function getStaticUrl() {
        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
    }
 
    public function getCurrentUrl() {
        return $this->_storeManager->getStore()->getCurrentUrl(false);
    }
 
    public function getBaseMediaDir() {
        return $this->_storeManager->getStore()->getBaseMediaDir();
    }
 
    public function getBaseStaticDir() {
        return $this->_storeManager->getStore()->getBaseStaticDir();
    }
}

Using template file

We also can use the functions in our view (.phtml) file as follows.

// to get base URL
echo $block->getBaseUrl();
  
// to get link URL
echo $block->getLinkUrl();
  
// to get media URL
echo $block->getMediaUrl();
  
// to get static URL
echo $block->getStaticUrl();
  
// to get current URL
echo $block->getCurrentUrl();
  
// to get media directory path
echo $block->getBaseMediaDir();
 
// to get static directory path
echo $block->getBaseStaticDir();

Using Object Manager (Not recommed)

Here is the example code to get the store URLs in Magento 2 using object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$store = $storeManager->getStore();
 
// to get base URL
echo $store->getBaseUrl();
 
// to get link URL
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);
 
// to get media URL
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
 
// to get static URL
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
 
// to get current URL
echo $store->getCurrentUrl();
 
// to get custom URL
echo $store->getUrl('faq/index');

Hope it helps. If you have an any ideal about our modules : Magento 2 Multi Vendor Marketplace Extension, Booking System Pro, Online product designer, you can comment under this post. I hope you will build great Magento 2 Extension for your website and Project