Get the Base Url

Getting the Base URL of the current store in Magento 2 is simply as that!Let's have a look together.

In our example we have create a helper class where we instantiate the Magento\Store\Model\StoreManagerInterface from which we can use the get the Base URL and in furthermore other store urls like the current URL, the Media Base URL, the Static Base URL and so on.

The fucntion getStore() can take as paremeter the store Id in case we want the get the Base URL from a specific store.

Also depending the URL we want to get, we can pass additional parameters to the getBaseUrl() function, which are the link type and a flag (true / false /null) if you want to get the secure URL, like we see on the example above. All the different link types can be found in the Magento\Framework\UrlInterface interface and the declaration of the getBaeUrl() function can be found in the Magento/Framework/Url/ScopeInterface interface. By default is the type URL_TYPE_LINK used and the flag to secure URL is null.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge