Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

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();

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()

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!

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]);
    }

You Try this for product Image

$this->helper('Magento\Cms\Helper\Wysiwyg\Images')->getBaseUrl().'catalog/product'.$_product->getImage();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top