Magento Magento2 Tips & Tricks

How to Get Base URL in Magento2

How to Get Base URL in Magento2 July 27, 2017Leave a comment

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

In this tutorial, you will go through how to get base url in Magento 2. There are several ways to get the base url of your Magento 2 website.

If you want to get Base URL , then you can try below code:

Get Base Url(Method 1):

$this->_storeManager->getStore()->getBaseUrl();

Get Base Url(Method 2):

You can also use the following code which gives you the base url as well.

$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')
->getStore($storeId)->getBaseUrl();

Get Media Base URL:

$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

Get Link Base URL:

$this->_storeManager->getStore()
->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);

Get Base URL With Template File(.phtml):

Also,you can get base url directly at phtml using direct call of object Manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storeManager->getStore()->getBaseUrl();

That’s it!

Hope this is useful for you 🙂

How to Get Base URL in Magento2
5 (100%) 2 votes

(Visited 2 times, 1 visits today)

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

Leave a Reply

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