How to get RefererUrl in magento 2 observer?
Make your observer class look like this:
namespace Vendor\Module\Observer;
class ClassNameHere implements \Magento\Framework\Event\ObserverInterface
{
protected $redirect;
public function __construct(
....
\Magento\Framework\App\Response\RedirectInterface $redirect,
...
) {
...
$this->redirect = $redirect;
....
}
}
then you can get the referer url in your execute
method like this
$redirectUrl = $this->redirect->getRedirectUrl();
yourObserverFile.php
class ClassNameHere implements \Magento\Framework\Event\ObserverInterface
{
protected $redirect;
public function __construct(
....
\Magento\Framework\App\Response\RedirectInterface $redirect,
...
) {
...
$this->redirect = $redirect;
....
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
return $redirectUrl = $this->redirect->getRefererUrl();
}
}
Following line will help you to get refferer Url.
$this->redirect->getRefererUrl();