1

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();
    }
}

2 Answers 2

1

Create files as below:

Softadroit/Prescription/etc/frontend/routes.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details.
*/
-->
<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>

Softadroit/Prescription/view/layout/prescription_index_save.xml

<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>

<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="checkout.save" template="Softadroit_Prescription::Success.phtml"/>
</referenceContainer>
</body>
</page>

Change in controller as below

<?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();
            
           
        }
       
        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}
1
2

check the below code

In controller:

<?php

namespace Softadroit\Prescription\Controller\Index;

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

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Framework\View\Result\PageFactory $pageFactory,
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_pageFactory          = $pageFactory;
        $this->cartFactory           = $cartFactory;
        $this->resultRedirectFactory = $resultRedirectFactory;

        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();

        }

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('prescription/index/success');
        return $resultRedirect;
    }
}
7
  • I updated my code you can please check the question I update my code here have a look, it doesnot redirecting to that phtml file Aug 26, 2020 at 5:35
  • You want to call a phtml file from controller or you want to redirect to some page (i.e layout) ? Aug 26, 2020 at 5:37
  • After click on button I want the user to redirect to this file Success.phtml Aug 26, 2020 at 5:38
  • ok in which layout you have called success.phtml ? Aug 26, 2020 at 5:41
  • checkout_onepage_success.xml ``` <?xml version="1.0"?> <body> <referenceContainer name="order.success.additional.info"> <block class="Softadroit\Prescription\Block\Success" name="softadroit.order.success" template="Softadroit_Prescription::order/success.phtml" after="-"> </block> </referenceContainer> </body> ``` Aug 26, 2020 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.