Magento, Magento 2

How to Get Base URL and Current URL in Magento 2

How to Get Base URL and Current URL in Magento 2

In this tutorial, Today I will explain to how to get store base URL and current URL in Magento 2. Magento 2 get base URL is require many times at development time. So, Let’s follow the below code :

You may also like this :

There are 2 ways :

1) Construct Method :

<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\Block;

class Helloworld extends \Magento\Framework\View\Element\Template
{
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlInterface;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Store\Model\StoreManagerInterface       $storeManager
     * @param \Magento\Framework\UrlInterface                  $urlInterface
     * @param array                                            $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlInterface,
        array $data = []
    )
    {
        $this->storeManager = $storeManager;
        $this->urlInterface = $urlInterface;
        parent::__construct($context);
    }

    public function getStoreUrlData()
    {
        // base url
        $storeUrl = $this->storeManager->getStore()->getBaseUrl();

        // get link url of store
        $storeLinkUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);

        // store url without index.php
        $storeUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

        // get pub static url
        $storeStaticUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC);

        // get media base url
        $storeMediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);

        // get current page url
        $currentUrl = $this->urlInterface->getCurrentUrl();

    }
}

2) Object Manager Method :

/**
 * Created By : Rohan Hapani
 */
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$urlInterface = $objectManager->get('\Magento\Framework\UrlInterface');


echo $storeManager->getStore()->getBaseUrl();  // base url
echo $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);  // get link url of store
echo $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); // store url without index.php
echo $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_STATIC); //get pub static url
echo $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); // get media base url
echo $urlInterface->getCurrentUrl(); // get current page url

Here, I added example of base url, current page url, pub media url & pub static url. You can use it by add code in block file and then, call function in phtml file.

There are also second method to use code with object manager. But, I do not recommended to use code by object manager. So, I would like to suggest to use code by add in block and then, call function in phtml file or any other files.

That’s it !!!

I hope this blog is easy to understand about how to get store base URL and current URL in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Stay Safe and Stay Connected !!

 

Tagged ,