Magento 2How To GuideApril 17, 2024Simon Walker

How to Get Media URL in Magento 2?

How to Get Media URL in Magento 2?

In the world of eCommerce, Magento 2 stands out as the top preferred platform due to its versatility, customisability, and features. Given the drastic increase in online stores, differentiating one’s store from the competition can prove challenging. Competing on product quality is one option but certainly not easy.

Based on our experience as a leading Magento development company and extensive research, we can say with 100 per cent certainty that images and videos can provide the necessary edge. High-quality visuals can make the product pages and overall store engaging. This can boost customer satisfaction and leads.

This brings us to our topic for today – get media URL in Magento 2. In one of our previous posts, we saw how to get base URL in Magento 2? Here, we will see how to get media URL in Magento 2? The concept is pretty much the same. We will use dependency injection method as using object manager is not recommended.

Check Out:

Get Media URL in Magento 2

We will be using Dependency Injection Method to get media URL in Magento 2. To do this, we've to use the Block class with __construct() function and inject the object of StoreManagerInterface in the constructor.

Create __construct function in your phtml Block file as below.
public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
{
       $this->_storeManager = $storeManager;
}

Get Media URL in phtml File

Now in your phtml file, call below method to get media URL.

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

Get Media URL in Magento 2 Using the Object Manager

As leading Magento development experts, we do not recommend this method unless you are an advanced-level Magento expert. The direct and frequent use of the object manager can create compatibility issues. You will notice these issues during upgrades or installing extensions.

Secondly, using this method means creating tightly coupled code. This can create problems during testing phase. Maintaining this code can become a challenge especially if you keep changing developers. Lastly, tightly coupled code has little reuse value since components do not work independently.

If you still wish to opt for the object manager method, then use the below code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$mediaUrl = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
echo $mediaUrl;

Final Thoughts on Magento 2 Get Media URL

That’s it for this tutorial on how to get media URL in Magento 2. If you have any Magento (1 or 2) related queries, then ask our expert Magento consultants. Our passionate and extremely talented Magento team will resolve your query promptly and to your satisfaction.