How to get RefererUrl in magento 2 observer?
-
06-10-2020 - |
Question
How to get RefererUrl (source page url) in Observer file?
Actually by using this;
$this->_redirect->getRefererUrl()
;
I am getting Referer Url in magento 2 controller file.But in Observer file I am not getting the source url (RefererUrl).
Can you help me on this ?
Solution
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();
OTHER TIPS
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange