Question

If anyone have idea please guide me, below is my code:

<?php
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\Controller\ResultFactory; 
    class Xyz extends \Magento\Framework\App\Action\Action
    {
        public function __construct(
        \Magento\Framework\App\Action\Context $context
        ) {

        parent::__construct($context);
        }

        public function execute()
        {   

                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
                $resultRedirect->setUrl($this->_redirect->getRefererUrl());
                return $resultRedirect;
                // $this->_redirect->getRefererUrl() -> return with 302 status code
        }
      }
?>
Was it helpful?

Solution

Try this:

<?php
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context; 
    use Magento\Framework\Controller\ResultFactory; 
    class Xyz extends \Magento\Framework\App\Action\Action
    {
        protected $redirect;
        public function __construct(
        Context $context,
        \Magento\Framework\App\Response\RedirectInterface $redirect
        ) 
        {

        $this->redirect = $redirect;
        parent::__construct($context);
        }

        public function execute()
        {   
            $redirectUrl = $this->redirect->getRedirectUrl();
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($redirectUrl);
            return $resultRedirect;
        }
}

OTHER TIPS

Try it this way using setRedirect an code 301:

public function execute()
{   
    $this->getResponse()->setRedirect($this->_redirect->getRefererUrl(), 301);
    return $this->resultPageFactory->create();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top