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->request = $request;
        $this->logger = $logger;
        $this->orderRepository = $orderRepository;
        $this->resultRedirectFactory = $resultRedirectFactory;
        $this->execute();
    }
 
    /**
     * Index action
     *
     * @return \Magento\Backend\Model\View\Result\Page
     */
    public function execute()
    { 
        $completeState = \Magento\Sales\Model\Order::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(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setPath('/sales/order/view/order_id/'.$order_id.'/');
            return $resultRedirect;
            exit;
          
    }
 
}
2

Your Answer

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

Browse other questions tagged or ask your own question.