21

How to redirect to previous page from my custom action in magento 2

4 Answers 4

58

In Your Controller write following code:

namespace Company\Module\Controller\Index;
use Magento\Framework\Controller\ResultFactory; 

class Actionname name extends \Magento\Framework\App\Action\Action
{      
    public function execute()
    {
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);

        // Your code

        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}

This code works for me.Hope it will help you too.

6
  • Is their need to be xml file related to action in my module @ND17 May 8, 2018 at 12:06
  • @PranayK First tell me what is your issue, If your action does not use for any page render than no need to add XML file
    – ND17
    May 8, 2018 at 13:21
  • if suppose i came to the list page when i need to go back, to home page i am getting the refererurl has list page url @ND17 May 8, 2018 at 13:26
  • When you need to go back any action are you perform? for example click on something?
    – ND17
    May 8, 2018 at 13:29
  • yes I have Back link on click i need to get back to home page it is staying in list Page only. @ND17 May 8, 2018 at 13:36
21

This could also be a valid answer in a shorter form Magento 2.0.4

namespace Vendorname\Modulename\Controller\Adminhtml\Index;
class Fetch extends \Magento\Backend\App\Action
{
    public function execute()
    {
        // TODO: Implement execute() method.
        $this->_redirect($this->_redirect->getRefererUrl());
    }
}
3
  • The execute method is expected to return either a Magento\Framework\Controller\ResultInterface or a Magento\Framework\App\ResponseInterface. Luckily the _redirect method does one of those so this is advisable: return $this->_redirect($this->_redirect->getRefererUrl()); Jun 18, 2018 at 14:15
  • cool for the backend. works awesome with Magento 2.2.6. Feb 26, 2019 at 11:05
  • This is the correct method to redirect to previous page. Thank you
    – Avesh Naik
    May 8, 2019 at 5:05
4

Please use this code in your controller class:

$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setRefererUrl();
return $resultRedirect;

Here resultRedirectFactory is a class variable that can be inherited in your class from there parents.

To check if referral URL is set or not, Please print the $_SERVER variable first. This code is working for me.

2
public function execute(){
    $resultRedirect = $this->resultRedirectFactory->create();
    try{
        $this->messageManager->addSuccess(__('Redirection Sucessful.'));
    }catch (\Exception $e) {
        $this->messageManager->addError($e->getMessage());            
    }
    $resultRedirect->setUrl($this->_redirect->getRefererUrl());
    return $resultRedirect;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.