Magento 2

magento2 get base url and media url and static url

magento2 get base url and media url and static url

magento2 get base url and media url and static url. Whenever you are working on  magento2 then a lot of times you need to get base URL and media URL and static url. From this blog, you base url, media url, static content url in Magento2.Magento2 basically, use interface

And this class have a function  Store()  which is provide store data.

After that,  using getBaseUrl() over Store()  you can get base url  and also get Media URL.

Where $storeManager is the instance of Magento\Store\Model\StoreManagerInterface.

If you want to get Base Url without Index.php then click here.

For Getting Media Url Use Below code:

Get Static content Url

But I am strongly recommended to NOT use object Manager. Use Interface injection at the __construct function of your class. Like below example:

 

 

Want to get  Base Url with Out Index.php then you below code:

The above code always gives the URL without Index.php either Seo rewrite enabled at the admin or not.It does not depend on that setting.

Where,

It gives Base URL with index.php http://example.com/index.php/  when SEO rewrite is not enabled.

And give Base URL without index.php http://example.com/index.php/  when SEO rewrite is enabled.

 

magento2 get base url and media url and static url
magento2 get base url and media url and static url

One Comment

Leave a Reply

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