How To Get Base, Media, Link Url In Magento2
To get URL in magento2, you have to first create instance of \Magento\Framework\App\ObjectManager to 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:
1
2
3
|
$baseUrl = $currentStore ->getBaseUrl(); //Output link will be like this:
|
To get media base URL:
1
2
3
|
$mediaUrl = $currentStore ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); //Media url output link will be like this:
|
To get link base url:
1
2
3
|
$linkUrl = $currentStore ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); //Output link will be link this:
|