3

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
        }
      }
?>
0

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;
        }
}
  • its not working – Hitesh Balpande Mar 11 at 13:40
  • updated my answer – fmsthird Mar 12 at 3:14
  • still it is coming with 302 status code – Hitesh Balpande Mar 12 at 11:18
  • It works in me the above code – fmsthird Mar 12 at 11:38
  • can you tell me what is really the logic you need? because you can actually avoid 302 status by changing redirect to TYPE_PAGE – fmsthird Mar 12 at 12:09
0

Try it this way using setRedirect an code 301:

public function execute()
{   
    $this->getResponse()->setRedirect($this->_redirect->getRefererUrl(), 301);
    return $this->resultPageFactory->create();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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