How to echo or print the referer url(history page) from where I reached to that login page.
I have to implement something that after login user should not reach to dashboard instead he should re-direct to the same page from where he came after login.
Some blogs says its an inbuilt feature of magento , but i don't why this inbuilt feature is not working in my case and also i need to know the flow of the referer/key variable and its implementation to be successful.
I want to know the proper working or some good tutorial link for that referer implementation.
I tried this
My research page - https://www.rakeshjesadiya.com/get-referer-url-in-magento-2/
1 Answer
You must create a plugin to intercept the controller that does the login.
di.xml
<type name="\Magento\Customer\Controller\Account\LoginPost">
<plugin name="my_plugin_after_login" type="\MyVendor\MyModule\Plugin\LoginPostPlugin" sortOrder="1" />
</type>
And the plugin class
<?php
/**
*
*/
namespace MyVendor\MyModule\Plugin;
/**
*
*/
class LoginPostPlugin
{
/**
* @var \Magento\Framework\App\Response\RedirectInterface
*/
protected $redirectInterface;
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* LoginPostPlugin constructor.
* @param \Magento\Framework\App\Response\RedirectInterface $redirectInterface
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\App\Response\RedirectInterface $redirectInterface,
\Psr\Log\LoggerInterface $logger
) {
$this->redirectInterface = $redirectInterface;
$this->logger = $logger;
}
/**
* Change redirect after login to home instead of dashboard.
*
* @param \Magento\Customer\Controller\Account\LoginPost $subject
* @param \Magento\Framework\Controller\Result\Redirect $result
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function afterExecute(
\Magento\Customer\Controller\Account\LoginPost $subject,
$result
) {
$result->setPath($this->redirectInterface->getRefererUrl());
return $result;
}
}
The method getRefererUrl() from the interface \Magento\Framework\App\Response\RedirectInterface help you with that.