Home > Back-end >  Magento 2 Module Handling Displaying Data proccessed in Controller
Magento 2 Module Handling Displaying Data proccessed in Controller

Time:04-18

I have set up a Magento 2 module to collect some data from a user using a form. When the form is submitted, the controller should process that data, then display the result on another page. I'm having a problem getting the controller to redirect to the results page with the data. Here is what I currently have set up:

Block file vendor\MyModule\Block\Index.php

    public function getManualFormAction()
    {
        return $this->getUrl('mamform/index/post');
    }

Template file vendor\MyModule\view\frontend\templates\index.phtml

<form action="<?= $block->escapeUrl($block->getManualFormAction()) ?>" >
...
</form>

Controller file vendor\MyModule\Controller\Index\Post.php

public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        var_dump($post);

        // handle and proccess data
       
        // The data processed should be displayed in the Result page
    }

The Results Controller vendor\MyModule\Controller\Index\Result.php

public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }

Results template vendor\MyModule\view\frontend\templates\result.phtml

<h1>Results appears here</h1>

Module Routes vendor\MyModule\etc\frontend\routes.xml

<?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 frontName="mamform" id="mamform">
            <module name="vendor_MyModule" />
        </route>
    </router>
</config>

Result Layout vendor\MyModule\view\frontend\layout\mamform_index_result.xml

<?xml version="1.0"?>
<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  name="mam_result" template="vendor_myModule::result.phtml"></block>
        </referenceContainer>
    </body>
</page>

Currently if I visit /mamform/result , I can see the template working as expected. How can I now pass the data proccessed in the Post Controller, and display it in the Result page?

CodePudding user response:

What you are looking for is a Redirect result.

In your Post controller you can create the redirect object like so:

<?php
public function execute() {
    $post = $this->getRequest()->getPostValue();
    // handle and process data
    // The data processed should be displayed in the Result page
    $result = $this->resultRedirectFactory->create();
    $result->setPath("path to your result controller", [$post, any_other_params]);
    return $result;
}
?>

This will create the redirect result and will call the execute method of whatever controller route you passed as the first param of setPath and the array will be passed in with the url.

  • Related