How to implement a redirect to a custom success page after submitting a form on a custom page (similar functionality like checkout)

magento.stackexchange https://magento.stackexchange.com/questions/307068

Question

I have a submission form and a controller, with the execute method. At the moment, after sending the form, my page reloads and on top of the page I get an alert that notifies the user about the success or failure of the operation, how can I instead redirect to a custom success page?

The execute method of my controller app/code/Vendor/Module/Controller/Order/Create.php:

    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $data = $this->getRequest()->getPostValue();
        $sampleIds = explode(',', $data['sample_ids']);
        if (count($sampleIds) >  $this->helper->getMaxCount()) {
            $this->messageManager->addError(__('The count of "samples" exceeds the limit'));
            $data = null;
        }

        if ($data) {
            /** @var \Vendor\Module\Model\Order $model */
            $model = $this->_objectManager->create('Vendor\Module\Model\Order');
            $model->setData($data);
            $model->setStoreId($this->_storeManager->getStore()->getId());

            try {
                $this->objectRepository->save($model);
                $this->messageManager->addSuccess(__('Request was added to processing.'));
                $this->dataPersistor->clear('vendor_sample_order');

                // Send email
                $this->helper->sendEmail($model);

            } catch (\Magento\Framework\Exception\LocalizedException $e) {
                $this->messageManager->addError($e->getMessage());
            } catch (\Exception $e) {
                $this->messageManager->addException($e, __('Something went wrong while saving the data.'));
            }
        }
        return $resultRedirect->setRefererOrBaseUrl();
    }
Was it helpful?

Solution

You can use the below code in your custom controller in execute method to redirect on any pages.

public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('routename/controllerpath/controllername');
return $resultRedirect;
}

No need to declare $this->resultRedirectFactory in the construct as its auto declared in the construct of \Magento\Framework\App\Action\Action to which your custom controller should extend. Otherwise, you'll get an error during compilation: resultRedirectFactory is already declared

Hope this helps.

OTHER TIPS

Try this code

$this->_redirect("your/url/here");

You can try this code

return $resultRedirect->setPath('your_path');

Try this:

 $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('yourpath');
    return $resultRedirect;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top