Magento2 Media path in .phtml file

I tried many options in magento2 to find the media channel url

How to find the URL of a media path in a template.

I tried

$object_manager = Magento\Core\Model\ObjectManager::getInstance();
$dir = $object_manager->get('Magento\App\Dir');
$mediaUrl = $dir->getDir(\Magento\App\Dir::MEDIA);

But it gives me an error class not found. I have a search and found one solution to create a function in a block file, but there are so many places where I want to use the media path in my design.

Thanks in advance.

+4
source share
8 answers

This is how you get the media path from within a block or template.

$this->getUrl('pub/media')
+3
source

The correct way to get media in PHTML:

$block->getViewFileUrl('images/myimage.png');
+1
source
//Case 1: $objectManager outside magento
use Magento\Framework\App\Bootstrap;    
include('your-path-to-/app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();


//Case 2: $objectManager inside magento
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media');
$mediaUrl = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);


// Case 3: Inside model
public function __construct(
\Magento\Framework\View\Element\Template\Context $context, 
\Magento\Framework\App\Filesystem\DirectoryList $directory_list, 
\Magento\Store\Model\StoreManagerInterface $url, 
array $data = []) {
    parent::__construct($context, $data);
    $this->directory_list = $directory_list;  
    $this->url = $url;  
}
$this->directory_list->getRoot();//root folder path
$this->directory_list->getPath('media');//media folder path
$this->url->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

.

+1

:

$object_manager = \Magento\Framework\App\ObjectManager::getInstance();
$dir = $object_manager->get('Magento\App\Dir');

$mediaUrl = $dir->getDir(\Magento\App\Dir::MEDIA);
0

- PHTML:

    $om = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $om->get('\Magento\Store\Model\StoreManagerInterface');
    var_dump($storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA));

index.php

:

define('MAGENTO_ROOT', getcwd());

- :

$mediaPath = MAGENTO_ROOT.'/pub/media/';
0

Well, in my case, it worked:

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
    $store = $storeManager->getStore();
    $mediaUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
?>
0
source

$baseurl=$block->getUrl();

Returns your baseurl inside phtml.

0
source

Try to get it using StoreManagerInterface

use Magento\Store\Model\StoreManagerInterface;

protected $storeManager;

public function __construct(
    StoreManagerInterface $storeManager,
)
{
    $this->storeManager = $storeManager;
}

Now download the url using

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

All Articles