Magento 2 code how to redirect to previous page url in Magento2

Magento 2  code how to redirect to previous page url in Magento2


In magento 2 when a customer login he will be redirected to the account page , for the customer to reutn to the previous page from where he has logged in we will need to do the changes in the code for the customer to redirect to the previous page  magento 2 redirect to login page or magento 2 redirect after login  or magento 2 redirect to homepage or magento 2 redirect to previous page after login



<?php
namespace Free\Samaya\Controller\Time;

class Index extends \Magento\Framework\App\Action\Action
{





  public function __construct( 
        PageFactory $resultPageFactory
    ) { 
...................
        $this->resultPageFactory = $resultPageFactory;
         ...................
...................
    }

public function execute()
    {
$urlToRedirect = $this->resultRedirectFactory->create();
$backUrl = $this->getBackUrl($this->_redirect->getRefererUrl());
                $urlToRedirect->setUrl($backUrl);
                return $urlToRedirect;
}






Comments