I have a controller and from there I want to redirect to a custom phtml file.
My controller path is app/code/Softadroit/Prescription/Controller/index/Save.php
The phtml file is stored in app/code/Softadroit/Prescription/view/frontend/templates/Success.phtml
How can I redirect to this page from my controller?
This is my controller code but it is not redirecting to that phtml file:
<?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/success');
//$resultRedirect->setPath('checkout/onepage/successs');
return $resultRedirect;
}
$this->_view->loadLayout();
$this->_view->renderLayout();
}
}