How to get the media folder URL programmatically in Magento 2
Magento 2 uses the pub/media folder for storing images and other media data. The following function returns its url for the current store:
1 2 3 4 5 6 7 8 9 10 |
/** @return string */ function getMediaBaseUrl() { /** @var \Magento\Framework\ObjectManagerInterface $om */ $om = \Magento\Framework\App\ObjectManager::getInstance(); /** @var \Magento\Store\Model\StoreManagerInterface $storeManager */ $storeManager = $om->get('Magento\Store\Model\StoreManagerInterface'); /** @var \Magento\Store\Api\Data\StoreInterface|\Magento\Store\Model\Store $currentStore */ $currentStore = $storeManager->getStore(); return $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); } |
The getBaseUrl method is absent in \Magento\Store\Api\Data\StoreInterface. It is represented only in particular interface implementations.