Magento – Magento2 : How to make a redirect with params

magento2post-dataredirect

I have a form with several inputs.(text and file).

    <form id="form-configurator" class="form-configurator " name="formconfigurator" action="index/toto" method="post" enctype="multipart/form-data">
       <input id="input-file" name="file" type="file" class="input-text required-entry" accept=".txt"></input>
       <input id="input-foo" name="foo" type="text" class="input-text required-entry"></input>
       <input id="input-bar" name="bar" type="number" class="input-text required-entry"></input>
       <input name="submit" value="the Name" type="submit" class="button nav-anchor" id="OrderButton"></input>
    </form>

The form submission calls a controller in which I manage to get the form parameters.
At the end of the controller execution, I have redirect it to another page :

class toto extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;
    protected $_resulFactory;


    public function __construct(
            \Magento\Framework\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory,
            \Magento\Framework\Controller\ResultFactory $resulFactory,
            )
    {
        parent::__construct($context);
        $this->_resultPageFactory = $resultPageFactory;
        $this->_resultFactory = $resulFactory;
    }

    public function execute()
    {
        $post = $this->getRequest()->getPost();
    ....
    $resultRedirect = $this->_resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
                $resultRedirect->setPath('foo.html');
                return $resultRedirect;
}

The redirection works but I do not manage to pass parameters.

(I found an alternative solution by adding to the url ?params=, but i would like them to be hidden like in POST)

Thank you for your help

Best Answer

You can use the below code to pass parameters to any urls:

$param = ['param1' => 'value1', 'param2' => 'value2'];
$resultRedirect->setPath('frontname/path/controllername', ['params' => $params]);
Related Topic