Question

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.

Was it helpful?

Solution

   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();

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top