Magento 2 Get Base Url and Current Url – Complete Guide

In this article, you going to see the discussion regarding Magento 2 Get Base Url and Current Url in detail and step by step.

If you looking for this topic then you are at right place.

Let’s start with the topic of Magento 2 Get Base Url and Current Url.

It is very simple to get current and base Url.

You can get Url using the interface file called StoreManagerInterface file and UrlInterface file.

With help of this both file object, you can get Base Url, Current Url, get Media URL, get static page URL and custom Url.

Path for this file is given below.

Magento\Store\Model\StoreManagerInterface
Magento\Framework\UrlInterface

Use the StoreManager Object.

Next step is to create PHP file and passed it for dependency injection for interface file called StoreManagerInterface file.

Magento 2 get base url with store code.

Use the below code snippet.

For this you have to add the StoreManagerInterface in the construct.

public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;
    }

    public function getStoreManagerData()
    {
        return $this->storeManager->getStore();
    }

Use this interface method by calling its method in template file.

Magento 2 get Base Url in phtml

Use the below code for getting base url.

<?php 

	$storeManager = $block->getStoreManagerData();
 
    echo $storeManager->getBaseUrl();
          Output:
    http://www.techone.in/default/
 
    echo $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); 
        Output:
    http://techone.in/
 
    echo $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_DIRECT_LINK);
        Output:
    http://techone.in/

?>

Magento 2 get Media Url in phtml

Use the below code for media Url.

<?php 

	$storeManager = $block->getStoreManagerData();
	
	echo $storeManager->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); 
        Output:
     http://www.techone.in/pub/media/
	
?>

Magento 2 get Customer Account Url in phtml.

Below code snippet is for getting Customer Account Url in phtml

<?php 

	$storeManager = $block->getStoreManagerData();
	
	echo $storeManager->getUrl('customer/account/login');
        Output:
     http://www.techone.in/default/customer/account/login/
 
    For Set Secure Url,
    echo $storeManager->getUrl('customer/account/login', ['_secure' => true]);
        Output:
     http://techone.in/default/customer/account/login/
	
?>

Magento 2 get Current Url in phtml

Use the below script to achieve this.

<?php 

	$storeManager = $block->getStoreManagerData();
	
	 echo $storeManager->getCurrentUrl(false);
       Output:
     http://www.techone.in/default/tutorialexample/index/tutorialexample
 
	echo $storeManager->getCurrentUrl();
       Output:
    http://techone.in/default/tutorialexample/index/tutorialexample?___from_store=default
	
?>

Magento 2 get login Url.

<?php 

	$storeManager = $block->getStoreManagerData();
	
	echo $storeManager->getUrl('customer/account/login');
        Output:
     http://www.techone.in/default/customer/account/login/
 
    For Set Secure Url,
    echo $storeManager->getUrl('customer/account/login', ['_secure' => true]);
        Output:
     http://techone.in/default/customer/account/login/
	
?>

Magento 2 get Secure Url

<?php
	$storeManager = $block->getStoreManagerData();
	
	echo $storeManager->isUrlSecure();
    // false if URL is not secure, true if URL is secure
?>

Question : Magento 2 get Url in Plugin and Observer file

Use the UrlInterface Object in Plugin and Observer file.

Use the Below code snippet

<?php
	public function __construct(
    \Magento\Framework\UrlInterface $urlBuilder
	) {
		$this->urlBuilder = $urlBuilder;
	}
?>

Then call this in template file.

<?php 

    echo $this->urlBuilder->getCurrentUrl(); 
    Output:
    http://www.techone.in/tutorialexample/index/tutorialexample

    echo $this->urlBuilder->getUrl();
    Output:
    http://techone.in/default/

    echo $this->urlBuilder->getUrl('customer/account/login');
    Output:
    http://techone.in/default/customer/account/login/

    echo $this->urlBuilder->getBaseUrl(); 
    Output:
    http://techone.in/default/

?>

Magento 2 get Query Url parameters in phtml

It is very easy to add Query Url parameters.

Use below code to set Query Url parameters.

<?php
	$url = $this->urlBuilder->setQueryParam('queryone', 'valueone');
?>

For passing multiple query parameter you need to call again above line.

Please check with below code.

<?php
	public function getQueryParameter() {
        $url = $this->urlBuilder->setQueryParam('queryone', 'valueone');
        $url = $this->urlBuilder->setQueryParam('querytwo', 'valuetwo');
        return $url->getUrl('catalogsearch/result/index');
    }
?>

Use below code for getting the value in template file

<?php
	echo $block->getQueryParameter();
	Output:
	http://www.techone.in/default/catalogsearch/result/index/?queryone=valueone&querytwo=valuetwo
?>

Magento 2 CLI Commands List

Conclusion :

Using above code snippet you understand regarding Magento Get Base Url and Current Url.

I hope you like this article and if you have any query, please comment below.

Leave a Comment