24

How to Get Media URL in Template file ? All the solution i found is calling Object Manager directly. My another concern, Can you ever call object manager directly as best practice ? (because in most of the solution they are using object manager)

37

You can get media url in your template file using below way but without using objectmanager you must have to define Block file with __construct() method with define storeManagerInterface in construct method.

In your phtml Block file create __construct function.

public $_storeManager;

public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
{
       $this->_storeManager = $storeManager;
}

In your phtml file call below method to get mediaurl,

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

echo $mediaUrl;

This is the proper way to get media url in Magento 2.

3
  • 4
    I reckon it would be way better to make that variable protected and create a getter method to retrieve the media url instead Aug 25 '16 at 9:49
  • If your block extends from \Magento\Framework\View\Element\Template, you already have an instance of StoreManagerInterface ($this->_storeManager).
    – Erfan
    Sep 18 '17 at 3:00
  • I got media url . But i need to display the image corresponding to that url...If you know help me to sort out my problem. Please refer the below link magento.stackexchange.com/questions/201961/…
    – Jaisa
    Nov 18 '17 at 5:34
14

As of 2.1, there is no direct way of getting the media URL without either:

  • calling the object manager directly (please don't do that)
  • override the block and add a new method

Rakesh mentioned one way of doing it.

Another way is to use the protected variable $_urlBuilder which is included for every block as defined in the AbstractBlock : https://github.com/magento/magento2/blob/f2d309a88298886460351c04973a4ff95c7a91c0/lib/internal/Magento/Framework/View/Element/AbstractBlock.php#L186

Thus you don't have to modify the constructor of your block and can simply add the following method:

public function getMediaUrl() {
    return $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]);
}

Then in your template you can call:

$block->getMediaUrl();
3
  • 6
    i think it's absurd that i can't just retrieve media path in .phtml directly.
    – LucScu
    Feb 17 '17 at 9:37
  • 1
    @LucScu it might be discouraged, for sure. Probably not the best idea to reference an asset that has been uploaded and is not part of the codebase. It feels a bit nasty doing so.
    – Erfan
    Dec 28 '18 at 7:58
  • @LucScu it is absurd. Just use the object manager. There is no benefit of using the convoluted 'best' way just to get a url... This is the real world not 'best practice' world. 'Best practices' that Magento doesn't even follow lol. My goodness Magento really is trash I hate it so much 😑
    – 00-BBB
    Feb 11 '21 at 10:23
7

If you don't want to go to the trouble of extending \Magento\Framework\View\Element\Template, and you want your .phtml file to use the block \Magento\Framework\View\Element\Template, then you can use this shortcut:

$this->helper('Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl()
1
  • 1
    Using $this->helper() in PHTML files is deprecated and discouraged Jan 29 '20 at 5:10
6

I'm going to use Raphael's answer and extend it - but instead of adding the method into the block class, why not create a helper and add it in there?

Start by creating a new module the usual way, and within the root, create a new folder called "Helper" and add the required code in there:

namespace YourSite\YourModule\Helper;
use Magento\Framework\UrlInterface;
class Url extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function getMediaPath() {
        return $this->_urlBuilder->getBaseUrl(['_type' => UrlInterface::URL_TYPE_MEDIA]);
    }

}

Once activated and compiled, you will be able to use it within your template file using the following method:

<?php $url = $this->helper('\YourSite\YourModule\Helper\Url'); ?>
<img src="<?php echo $url->getMediaPath() ?>wysiwyg/image.jpg" />

I hope you (and potentially others) find this of great use!

1
  • Using $this->helper() in PHTML files is deprecated and discouraged Jan 29 '20 at 5:10
1

At least in 2.2.6, you can use Magento\Framework\UrlInterface::getDirectUrl()

    protected function buildMediaUrl($path)
    {
        return $this->urlBuilder->getDirectUrl( $path, ['_type' => UrlInterface::URL_TYPE_MEDIA]);
    }
-1

You Try this for product Image

$this->helper('Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl().'catalog/product'.$_product->getImage();
3
  • Call to helper in template file is deprecated Sep 27 '20 at 7:39
  • @VishwasBhatnagar you write this many places. Maybe you could suggest the "corret" way to do it as well?
    – william
    Apr 15 '21 at 13:53
  • 1
    @william Please refer to the accepted answer or top two answer to check the correct way Apr 16 '21 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.