Reading list Switch to dark mode

    How to Get Base,Media,Link url in Magento 2

    Updated 23 February 2024

    Magento-Code-Snippet-5

    To get the URL in magento2, you have to first create an instance of  \Magento\Framework\App\ObjectManager to get the 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 the base URL:

    $baseUrl = $currentStore->getBaseUrl();
    //Output link will be like this:
    //http://magento2.webkul.com/marketplace

    To get media base URL:

    $mediaUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
    //Media url output link will be like this:
    //http://magento2.webkul.com/marketplace/pub/media

    To get the link base URL:

    $linkUrl = $currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_LINK);
    //Output link will be link this:
    //http://magento2.webkul.com/marketplace

    I hope this blog will help you with How to Get Base, Media, Link url in Magento 2. You may also check our wide range of best Magento 2 Extensions.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Please reach out to our team via a support ticket if you have any queries.

    Try this and if you have any queries then just comment below 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Praveen Chelumalla
  • Kamlesh Kumar Jha
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home