Magento 2 get Base URL

Magento 2 get Base URL – As you know, in Magento 1 we can easily get base url using function:

In this tutorial, let’s find out how to get base url in Magento 2

In Magento 2, we can get base url using an instance of StoreManagerInterface 

1. Create an instance of StoreManagerInterface in __construct function.

Then we can get base url using function

We also can get url for media, static, js directory:

2. In another way, we can create an instance of StoreManagerInterface using ObjectManager:

That’s how to get base url in Magento 2. See you in the next tutorial.

Leave a Reply

Your email address will not be published.