13

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)

21

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.

  • 4
    I reckon it would be way better to make that variable protected and create a getter method to retrieve the media url instead – Raphael at Digital Pianism 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
13

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();
  • 4
    i think it's absurd that i can't just retrieve media path in .phtml directly. – LucScu Feb 17 '17 at 9:37
  • @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
3

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!

3

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

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

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.