How to Get Base URL, Media URL, Current URL, Base Path, Media Path – Magento 2.x

In this blog, both methods are demonstrated, using Dependency Injection and using ObjectManager.

Using Dependency Injection

Here is the sample code. It has been used in a block class but you may use it in any class you want. We will be using three classes:

\Magento\Store\Model\StoreManagerInterface
\Magento\Framework\Filesystem\DirectoryList and
\Magento\Framework\UrlInterface as follows:

<?php 
namespace Vendor\Module\Block;

class DataBlock extends \Magento\Framework\View\Element\Template {

    protected $_storeManager; 
    protected $_urlInterface; 
    protected $_dir; 

    public function __construct(
        \Magento\Backend\Block\Template\Context $context, 
        \Magento\Store\Model\StoreManagerInterface $storeManager, 
        \Magento\Framework\Filesystem\DirectoryList $dir, 
        \Magento\Framework\UrlInterface $urlInterface, 
        array $data = [] 
    ) { 
        $this->_storeManager = $storeManager; 
        $this->_dir = $dir; 
        $this->_urlInterface = $urlInterface; 
        parent::__construct($context, $data); 
    } 
    /** 
     * This function prints all of the required data using: 
     * \Magento\Store\Model\StoreManagerInterface 
    */ 
    public function getStoreManagerData() { 

        echo $this->_storeManager->getStore()->getId() . '<br />';

        /**#@+ Standard Function Call: 
        $this->_storeManager->getStore()->getBaseUrl(Url type);
        /*Possible URL types const URL_TYPE_LINK = 'link'; 
        const URL_TYPE_DIRECT_LINK = 'direct_link'; 
        const URL_TYPE_WEB = 'web'; const URL_TYPE_MEDIA = 'media'; 
        const URL_TYPE_STATIC = 'static'; 
        const URL_TYPE_JS = 'js'; 
        */ 
        // 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 />'; 
        /* To get custom URL based on url key */ 
        echo $this->_storeManager->getStore()->getUrl('url_key') . '<br />'; 
        /* To get Current URL */ 
        echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />'; 
        echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />'; 
        echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />'; 
    }

    /** 
    * This function prints all of the required data using: 
    * \Magento\Framework\UrlInterface 
    */ 
    public function getUrlInterfaceData() { 
        echo $this->_urlInterface->getCurrentUrl() . '<br />'; 
        echo $this->_urlInterface->getUrl() . '<br />'; 
        /* To get custom URL based on url key */ 
        echo $this->_urlInterface->getUrl('url_key') . '<br />'; 
        echo $this->_urlInterface->getBaseUrl() . '<br />'; 
    } 
    
    /** 
     * This function prints all of the directory paths using: 
     * \Magento\Framework\Filesystem\DirectoryList 
    */ 
    public function getDirectoryPaths() { 
        echo $this->_dir->getRoot()."<br>"; // Output: /var/www/html/myproject 
        echo $this->_dir->getPath('media')."<br>"; // Output: /var/www/html/myproject/pub/media 
        echo $this->_dir->getPath('pub')."<br>"; // Output: /var/www/html/myproject/pub 
        echo $this->_dir->getPath('static')."<br>"; // Output: /var/www/html/myproject/pub/static 
        echo $this->_dir->getPath('var')."<br&>"; // Output: /var/www/html/myproject/var 
        echo $this->_dir->getPath('app')."<br>"; // Output: /var/www/html/myproject/app 
        echo $this->_dir->getPath('etc')."<br>"; // Output: /var/www/html/myproject/app/etc 
        echo $this->_dir->getPath('lib_internal')."<br>"; // Output: /var/www/html/myproject/lib/internal
        echo $this->_dir->getPath('lib_web')."<br>"; // Output: /var/www/html/myproject/lib/web 
        echo $this->_dir->getPath('tmp')."<br>"; // Output: /var/www/html/myproject/var/tmp 
        echo $this->_dir->getPath('cache')."<br>"; // Output: /var/www/html/myproject/var/cache 
        echo $this->_dir->getPath('log')."<br>"; // Output: /var/www/html/myproject/var/log 
        echo $this->_dir->getPath('session')."<br>"; // Output: /var/www/html/myproject/var/session 
        echo $this->_dir->getPath('setup')."<br>"; // Output: /var/www/html/myproject/setup/src 
        echo $this->_dir->getPath('di')."<br>"; // Output: /var/www/html/myproject/var/di 
        echo $this->_dir->getPath('upload')."<br>"; // Output: /var/www/html/myproject/pub/media/upload
        echo $this->_dir->getPath('generation')."<br>"; // Output: /var/www/html/myproject/var/generation 
        echo $this->_dir->getPath('view_preprocessed')."<br>"; // Output: /var/www/html/myproject/var/view_preprocessed 
        echo $this->_dir->getPath('composer_home')."<br>"; // Output: /var/www/html/myproject/var/composer_home 
        echo $this->_dir->getPath('html')."<br>"; // Output: /var/www/html/myproject/var/view_preprocessed/html 
    } 
}

You can see more functions in the following classes:
\Magento\Store\Model\Store
\Magento\Framework\UrlInterface

Using ObjectManager

Well, this approach is never recommended as you know that using ObjectManager is never recommended in Magento 2. Find more about this here.

Though, we will see some of the methods to get work done:

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $appState = $objectManager->get('\Magento\Framework\App\State'); 
    $appState->setAreaCode('frontend'); 
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface'); 
    $store = $storeManager->getStore(); 
    echo $store->getUrl('product/33'); 
    echo '<br>'; 
    echo $store->getCurrentUrl(); 
    echo '<br>'; 
    echo $store->getBaseUrl(); 
    echo '<br>'; 
    echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); 
    echo '<br>'; 
    echo $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); 
    echo '<br>'; 
?>

If you need any more help, feel free to contact me. Happy Coding 🙂

Updated: