Published on

How to get base, media and link url in magento2

  • avatar
    Bal Singh

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: //

To get media base URL:

$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL\_TYPE\_MEDIA); //Media url output link will be like this: //
$linkUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL\_TYPE\_LINK); //Output link will be link this: //