How To Get Base URL in Magento 2.x

There are following below two ways to get Base URLs

[1] By Using DI (Dependency Injection)

[2] By Using Object Manager

[1] By Using DI (Dependency Injection) : You can get Base URL, with / without Index, Store URL, Link URL, Media URL

[1.1] Write below code inside custom module Helper as

         [VendorName][ModuleName]\Helper\Data.php
<?php

namespace [VendorName]\[ModuleName]\\Helper;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\Response\RedirectInterface;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
  protected $storeManager;
 

  public function __construct( \Magento\Framework\App\Helper\Context $context,
                               StoreManagerInterface $storeManager
							 ) 
     {
      $this->storeManager = $storeManager;
      parent::__construct($context);
     }
    
    

  public function getStoreManagerData()
    {
  	    // To get Store Url with index.php
        return $storeUrl = $this->storeManager->getStore()->getBaseUrl();
        // To get Store Url without index.php
        return $storeUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
        // To get Link Url of store
         $storeLinkUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);
        // To get media Base Url
         $storeMediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
        //To get Static content Url
         $storeStaticUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);
    }
    
    
    
    
    
    
}
[1.2] Write below code inside custom module Block as

       [VendorName]\[ModuleName]\Block\Index\Index.php
<?php
namespace [VendorName]\[ModuleName]\Block\Index;
class Index extends \Magento\Framework\View\Element\Template
{
	protected $helper;
	
	public function __construct(
		\Magento\Framework\View\Element\Template\Context $context,
		[VendorName]\[ModuleName]\Helper\Data $helperData,
		
	)
	{
		$this->helper = $helperData;
		parent::__construct($context);
	}
		
	public function getbaseurldata()
    {
    return $this->helper->getStoreManagerData();
    }
	
	
}



[1.3] Write below code inside Custom Module Template Index phtml file
 [VendorName]\[ModuleName]\view\frontend\templates\index\index.phtml
$baseurl= $block->getbaseurldata();

Finally you can Base URL as per store need.

[2] By Using Object Manager :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
 
$storeManager->getStore()->getBaseUrl();  // to get Base Url
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // to get Base Url without index.php
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); // to get store link url
$storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); // to get Base Media url

Leave a Reply

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