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 URLs in Magento 2 using dependency injection.

In order to get the store URLs in Magento 2, 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\Framework\View\Element\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 (i.e, http://www.domainname.com/)
echo $block->getBaseUrl();
 
// to get link URL (i.e, http://www.domainname.com/)
echo $block->getLinkUrl();
 
// to get media URL (i.e, http://www.domainname.com/pub/media/)
echo $block->getMediaUrl();
 
// to get static URL (i.e, http://www.domainname.com/pub/static/version1548921909/)
echo $block->getStaticUrl();
 
// to get current URL (i.e, http://www.domainname.com/?___store=default&___from_store=default)
echo $block->getCurrentUrl();
 
// to get media directory path (i.e, pub/media)
echo $block->getBaseMediaDir();

// to get static directory path (i.e, pub/static)
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 (i.e, http://www.domainname.com/)
echo $store->getBaseUrl();

// to get link URL (i.e, http://www.domainname.com/)
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);

// to get media URL (i.e, http://www.domainname.com/pub/media/)
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

// to get static URL (i.e, http://www.domainname.com/pub/static/version1548921909/)
echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);

// to get current URL (i.e, http://www.domainname.com/?___store=default&___from_store=default)
echo $store->getCurrentUrl();

// to get custom URL (i.e, http://www.domainname.com/faq/index/)
echo '<br>' . $store->getUrl('faq/index');

// to get media directory path (i.e, pub/media)
echo '<br>' . $store->getBaseMediaDir();

// to get static directory path (i.e, pub/static)
echo '<br>' . $store->getBaseStaticDir();

Hope this helps.

Leave a Reply

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