Tuesday, July 5, 2022
HomeMagentoMagento 2 get Referrer Url

Magento 2 get Referrer Url

In this article, you going to see discussion regarding Magento 2 get Referrer Url in detail and step by step in a simple way.

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

Let’s start with the topic of discussion regarding Magento 2 get Referrer Url.

It is very simple to get Referrer Url in case Magento.

You can get this Referrer Url in controller, Phtml.

Similarly, you can also get in Observer.

You have to just use a method or a function called getRefererUrl() function or getRedirectUrl() function.

And this above function you can get by using interface called Redirect Interface Object.

The path of this interface which is used for fetching the Referrer URL is given below.

Path is  Magento\Framework\App\Response\RedirectInterface.

In case of PHP you can use simple code which given below to get referral url.

$_SERVER["HTTP_REFERER"].

But in case of Magento, the best way is to just call the interface in __construct() function and fetch the referrer URL.

Below is code snippet to referral Url.

public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect
) {
    $this->redirect = $redirect;
}

In function, then you can call below line to get referrer Url.

Use the below code for it.

echo $this->redirect->getRefererUrl();
echo $this->redirect->getRedirectUrl();

Magento 2 get Referrer Url in Controller

It is simple to get referer url in controller.

To achieve this you have to use the RedirectInterface.

You can refer the above code which is used in the construct function.

Check with the below code get the referrer url in your execute method like this.

$redirectUrl = $this->redirect->getRedirectUrl();

Magento 2 Get Base Url and Current Url – Complete Guide

Conclusion :

Finally we done with the point of getting Referrer Url.

I hope you like this article and if you think that i have missing something, please comment below.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular