How to get magento2 base url

How to get magento2 base url.

if want How to get magento2 base url for your current store then you can follow this post.

In several cases, you need to current store base Magento 1.x version you can get base url using Mage::getBaseUrl();

As Magento2, not used factory patterns, so there have no code like  Mage::getBaseUrl(); by which you can base URL.But at Magento2, you should use StoreManager object   then using  $this->_storeManager->getStore()->getBaseUrl()  you can get base url.

Magento2, use interface, injection  of class, that you should  Inject

at Model, Resource model, Helper classes for getting base url.

In below example class, I have injected  StoreManagerInterface  my class



Note that: if you have used any block class which extends base block template

and should avoid  injected of  store manager interface for that case as extends class already have store Manager object

try below code to get magento2 base URL:



How to get magento2 base url by Object Manager

Using Object manager for getting magento2 base URL then try below once:

Assume that your class has object manager.

If don’t have object manager then try this:

But this type of object manager calls is really bad idea.








Leave a Reply

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