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 MagentoBackendAppAction;
use MagentoFrameworkAppRequestInterface;
use VendorModuleLoggerLogger;
use MagentoSalesApiOrderRepositoryInterface;
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,
MagentoFrameworkControllerResultFactory $resultRedirectFactory,
MagentoBackendAppAction $context
) {
parent::__construct($context);
$this->request = $request;
$this->logger = $logger;
$this->orderRepository = $orderRepository;
$this->resultRedirectFactory = $resultRedirectFactory;
$this->execute();
}
/**
* Index action
*
* @return MagentoBackendModelViewResultPage
*/
public function execute()
{
$completeState = MagentoSalesModelOrder::STATE_COMPLETE;
$order_id = $this->request->getParam('order_id');
$order = $this->orderRepository->get($order_id);
$order->setStatus($completeState)->setState($completeState);
$this->orderRepository->save($order);
$resultRedirect = $this->resultRedirectFactory->create(MagentoFrameworkControllerResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('/sales/order/view/order_id/'.$order_id.'/');
return $resultRedirect;
exit;
}
}