Magento 2: Get Base URL, Store URL and Current URL

In this code snippet, I am going to show you how to get base URL, store URL and current URL in Magento 2. In the below example, I will be using both Dependency Injection (DI) and Object Manager to get the base URL, store URL and current URL in Magento 2. But Magento recommends using the dependency injection instead of object manager.

Using Dependency Injection (DI)

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();
    }
}

Now, we 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

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 this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *