0

I want to redirect to Success.phtml file from this controller

This is my controller code:

<?php

namespace Softadroit\Prescription\Controller\Index;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\App\ObjectManager;

class Save extends \Magento\Framework\App\Action\Action
{    
    protected $_pageFactory;

     public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Checkout\Model\CartFactory $cartFactory
        )
    {
        $this->_pageFactory = $pageFactory;
        $this->cartFactory = $cartFactory;
        return parent::__construct($context);
    }
    
    public function execute()
    {
        $post = $this->getRequest()->getPost();
        
        /* echo "<pre>";
        print_r($post);
        die(); */
        
        if (!empty($post)) {
        foreach($post['name'] as $key => $name) {
            $age    = $post['age'][$key];
            $weight = $post['weight'][$key];
            $height = $post['height'][$key];
            $gender = $post['subject_gender'][$key];
            $product_id = $post['product_id'][$key];
            $customer_id = $post['customer_id'][$key];
        
            $model = $this->_objectManager->create('Softadroit\Prescription\Model\Prescription');           
            
            $model->setData([
                    "name" => $name,
                    "age" => $age,
                    "weight" => $weight,
                    "height" => $height,
                    "gender" => $gender,
                    "product_id" => $product_id,
                    "customer_id" => $customer_id
                    ]);

             $msg ="";
             $data =  $model->save();
            if($data != ''){ 
                //$model->save(); 
                $this->messageManager->addSuccessMessage('Thanks your form is submitted successfully!');
            }else{ 
                $this->messageManager->addSuccessMessage('Error in submitting data. Please contact on this email!'); 
            } 
}
            echo $msg; 

            $cartObject = $this->cartFactory->create()->truncate();
            $cartObject->saveQuote();
            
            
            // Redirect to your form page (or anywhere you want...)
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl('/prescription/index');
            //$resultRedirect->setPath('checkout/onepage/successs');
            return $resultRedirect;
        }
       
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

And this is my Success.phtml file in template:

<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
            <p><?= echo escapeHtml(__('Your order has been placed successfully.'))?></p>
        
            <p><?= echo escapeHtml(__('We\'ll email you an order confirmation with details and tracking info.')) ?></p>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?= echo $baseUrl = $store->getBaseUrl(); ?>"><span><?= echo escapeHtml(__('Continue Shopping')) ?></span></a>
        </div>
    </div>
</div>

route.xml file

<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="prescription" frontName="prescription">
            <module name="Softadroit_Prescription" />
        </route>
    </router>
</config>

I have to create any other file? or this will work directly?

7
  • Can you share route.xml file? Aug 26 '20 at 8:54
  • @RakeshPatel updated route.xml file in question
    – Arun Kumar
    Aug 26 '20 at 9:50
  • which error you are facing with this one? Aug 26 '20 at 9:53
  • 404 Page not found
    – Arun Kumar
    Aug 26 '20 at 10:04
  • 1
    Can you use layout.xml file ? Aug 26 '20 at 12:42

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.