Magento 2 Admin Controller – Redirect URL with Parameters

admin-controllerparameterredirecturl

I am trying to redirect to a different controller from one controller . I also want to set an Url parameter along with the redirect. But this is throwing me an error

Uncaught Error: Call to undefined method
Magento\Backend\Model\View\Result\Redirect\Interceptor::setParam()

Here is my code.

public function execute()
{

    $resultRedirect = $this->resultRedirectFactory->create();
    $resultRedirect->setPath('customer/index/new');
    $resultRedirect->setParam('myname', 'Nuno Sousa');
    return $resultRedirect;
}

What is wrong with the above code? or is there any other way?

Best Answer

You can pass param like this :

$params = array('myname' => 'Nuno Sousa');
$resultRedirect->setPath('customer/index/new', ['params' => $params]);

You can retrive using this :

$this->getRequest()->getParams();