2

I'm trying to redirect from block but it's not working. But same methods work in different projects.

1. Process 1

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Vendor\PaymentGateway\Helper\Data $storeHelper,
    \Magento\Sales\Api\Data\OrderInterface $order,
    \Magento\Framework\App\Response\Http $response,
    \Magento\Sales\Model\Order $orderModel,      
    \Vendor\PaymentGateway\Model\CreateorderFactory $createorder,
    \Vendor\PaymentGateway\Model\PaymentresponseFactory $paymentResponse,
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Sales\Model\OrderFactory $orderFactory,
    \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderEmailSender,
    \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->storeHelper  = $storeHelper;
    $this->order = $order;
    $this->response = $response;
    $this->orderModel = $orderModel;
    $this->createorder = $createorder;
    $this->paymentResponse = $paymentResponse;
    $this->checkoutSession = $checkoutSession;
    $this->orderFactory = $orderFactory;
    $this->orderEmailSender = $orderEmailSender;
    $this->redirectFactory = $redirectFactory;
}
public function redirectAccordingToStatus($status='30')
{
    echo 'redirect from here  ' , $this->getBaseUrl().$this->REDIRECT_URL[$status] ;
    //return $this->redirectFactory->create()->setPath($this->REDIRECT_URL[$status]);
    $this->response->setRedirect($this->getBaseUrl().$this->REDIRECT_URL[$status]);
    exit;
}

2. Process 2: With same constructor but different approach.

return $this->redirectFactory->create()->setPath('paym/payment/cancel');

None of these methods are working. I thought some of the classes could be conflicting with it that is why I have pasted the whole code of constructor.

Also there is no error during setup:di:compile and in logs. If you know of any another approach then please suggest that one.

2 Answers 2

1
   protected $_responseFactory;
    protected $url;

    public function __construct(

        \Magento\Framework\App\ResponseFactory $responseFactory,

    ) {
        $this->_responseFactory = $responseFactory;
        $this->url = $context->getUrlBuilder();;
    }

USE LIKE THIS::

  $CustomRedirectionUrl = $this->url->getUrl('[ModuleName]/[ModuleName]/[[Action]');
                $this->_responseFactory->create()->setRedirect($CustomRedirectionUrl)->sendResponse();
2
  • Man it's working, but I got error in compilation. So would you help me with that as well. \Magento\Framework\UrlInterface already exists in context object
    – P S
    Jan 25, 2018 at 8:53
  • I've fixed the error with edited solution.
    – P S
    Jan 25, 2018 at 8:58
0

You can achieve this with the Process 2, with a slight change.

You have to use ResultFactory instead of RedirectFactory and you have to specify the type as redirect, so your code should be changed to

public function __construct(
    ................
    .......................
    \Magento\Framework\Controller\ResultFactory $resultFactory,
) {
    parent::__construct($context, $data);
    ........
    ..........
    $this->resultFactory = $resultFactory;
}
public function redirectAccordingToStatus($status='30')
{
    $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
    return $resultRedirect->setPath('paym/payment/cancel');
}

Run the setup upgrade again and check if it's working.

1
  • I tried , not working. I don't understan why, this is not working.
    – P S
    Jan 25, 2018 at 8:39

Your Answer

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

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