:::: MENU ::::
Posts tagged with: current url

Get base url current url in Magento 2

This tutorial is about Get base URL,current URL in Magento 2 .i.e how to get base URL current URL in Magento 2. you can also get media url and any custom url following this example. And in this tutorial, I will try to explain it briefly and in a simple way. I will share the code snippet below.

Get base url and current url in Magento 2

First lets note down the steps.

Step 1: Declare in (your module name)_HelloWorld
Step 2: Get current URL and base URL in the template (.phtml) file

Moving forward let us explain these steps step by step.

Declare in (your module name)_HelloWorld

You will use a block class of the module (your module name)_HelloWorld, then possibly inject the object of StoreManagerInterface and UrlInterface in the constructor of the module’s block class. You will work with two functions in the below class: getStoreManagerData() and getUrlInterfaceData().

  • In getStoreManagerData() function, you will use object of StoreManagerInterface to get the base and current url.
  • In getUrlInterfaceData() function, you will use object of UrlInterface to get the base and current url
  • .

    Open app/code/QaisarSatti/HelloWorld/Block/HelloWorld.php class and run the code:

    namespace QaisarSatti\HelloWorld\Block;
    class HelloWorld extends \Magento\Framework\View\Element\Template
            protected $_storeManager;
            protected $_urlInterface;
        public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Framework\UrlInterface $urlInterface,    
            $this->_storeManager = $storeManager;
            $this->_urlInterface = $urlInterface;
         * Printing URLs using StoreManagerInterface

        public function getStoreManagerData()
            echo $this->_storeManager->getStore()->getId() . '<br />';
            // by default: URL_TYPE_LINK is returned
            echo $this->_storeManager->getStore()->getBaseUrl() . '<br />';        
            echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB) . '<br />';
            echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK) . '<br />';
            echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . '<br />';
            echo $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC) . '<br />';
            echo $this->_storeManager->getStore()->getUrl('product/33') . '<br />';
            echo $this->_storeManager->getStore()->getCurrentUrl(false) . '<br />';
            echo $this->_storeManager->getStore()->getBaseMediaDir() . '<br />';
            echo $this->_storeManager->getStore()->getBaseStaticDir() . '<br />';    
         * Printing URLs using URLInterface

        public function getUrlInterfaceData()
            echo $this->_urlInterface->getCurrentUrl() . '<br />';
            echo $this->_urlInterface->getUrl() . '<br />';
            echo $this->_urlInterface->getUrl('helloworld/general/enabled') . '<br />';
            echo $this->_urlInterface->getBaseUrl() . '<br />';

    Step 2:

    Get base URL and current URL in the template file

    Use the following piece of code:

    <?php echo $block->getUrl('hello/test'); ?>
    <?php echo $block->getBaseUrl(); ?>

    Furthermore, that’s it from this tutorial. I hope it serves the purpose. Since these are learning tutorials, please feel free to drop any suggestions or queries in the comments section. That will definitely be highly appreciated.

    Author: Qaisar Satti
    Category: Magento 2
    Last Modified: February 4, 2019