0

I'm trying to redirect a controller that is executed in Order Detail.

If I end the controller returning redirect like this, it throws an error:

return $resultRedirect;

If I end the controller with exit; it does not throws error but a blank page is displayed.

exit;

Here is my code:

<?php

use Magento\Backend\App\Action; use Magento\Framework\App\RequestInterface; use Vendor\Module\Logger\Logger; use Magento\Sales\Api\OrderRepositoryInterface;

class Pdf extends Action { /** * @var PageFactory */ protected $request; protected $logger; protected $orderRepository; protected $resultRedirectFactory;

/**
 * @param Context $context
 * @param PageFactory $resultPageFactory
 */

public function __construct(
    RequestInterface $request,
    Logger $logger,
    OrderRepositoryInterface $orderRepository,
    \Magento\Framework\Controller\ResultFactory $resultRedirectFactory,
    \Magento\Backend\App\Action $context
) {
    parent::__construct($context);
    $this-&gt;request = $request;
    $this-&gt;logger = $logger;
    $this-&gt;orderRepository = $orderRepository;
    $this-&gt;resultRedirectFactory = $resultRedirectFactory;
    $this-&gt;execute();
}

/**
 * Index action
 *
 * @return \Magento\Backend\Model\View\Result\Page
 */
public function execute()
{ 
    $completeState = \Magento\Sales\Model\Order::STATE_COMPLETE;

    $order_id = $this-&gt;request-&gt;getParam('order_id');

        $order = $this-&gt;orderRepository-&gt;get($order_id);
        $order-&gt;setStatus($completeState)-&gt;setState($completeState);
        $this-&gt;orderRepository-&gt;save($order); 


        $resultRedirect = $this-&gt;resultRedirectFactory-&gt;create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        $resultRedirect-&gt;setPath('/sales/order/view/order_id/'.$order_id.'/');
        return $resultRedirect;
        exit;

}

}

Oscar Vazquez
  • 553
  • 7
  • 37

0 Answers0