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.


Here is my code:


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
    ) {
        $this->request = $request;
        $this->logger = $logger;
        $this->orderRepository = $orderRepository;
        $this->resultRedirectFactory = $resultRedirectFactory;
     * 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);

            $resultRedirect = $this->resultRedirectFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
            return $resultRedirect;

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.