Published on

How to get base, media and link url in magento2

Authors
  • avatar
    Name
    Bal Singh
    Twitter

To get URLs in magento2, you have to create the instance of \Magento\Framework\App\ObjectManager and get current store

$_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); //instance of\Magento\Framework\App\ObjectManager $storeManager = $\_objectManager->get('Magento\Store\Model\StoreManagerInterface'); $currentStore = $storeManager->getStore();

To get base URL:

$baseUrl = $currentStore->getBaseUrl(); //Output link will be like this: //https://www.soscodes.com

To get media base URL:

$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL\_TYPE\_MEDIA); //Media url output link will be like this: //https://www.soscodes.com/pub/media
$linkUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL\_TYPE\_LINK); //Output link will be link this: //http://magento2.webkul.com/marketplace