Created
July 5, 2020 10:59
-
-
Save yogeshdubey2006/e08fc1a15f5bfadf38da916f8b39b5f7 to your computer and use it in GitHub Desktop.
Magento 2 - How to get base, media, link url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//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 | |
$baseUrl = $currentStore->getBaseUrl(); | |
//Output: https://www.example.com/ | |
// To get media base URL | |
$mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); | |
//Output: https://www.example.com/pub/media | |
// To get link base url: | |
$linkUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK); | |
//Output: https://www.example.com |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment