Passing parameters via redirect
-
12-11-2019 - |
Question
Is it possible to pass parameters via redirect? I tried a lot of options, but nothing seems to work. My latest approach is:
return $this->redirect(array('Users::helloworld', 'args' => array('id' => 'myId')));
then I created a route:
Router::connect('/Users/helloworld?id={:id}', array('controller' => 'Users', 'action' => 'helloworld'));
but all I get is users/helloworld/myId
Solution
args
is part of the routes and will be converted into an URL using the very generic route (not the one you created and don't require)
To get a query string, simply use the ?
key:
return $this->redirect(array(
'Users::helloworld',
'?' => array('id' => $myId)
));
// will use the route:
// /{:controller}/{:action}/{:args}
// and generate
// /users/helloworld?id=$myId
The test for that: https://github.com/UnionOfRAD/lithium/blob/master/tests/cases/net/http/RouteTest.php#L374-405
OTHER TIPS
Instead of defining a separate route to pass arguments, you could just do the following. Lets say you want to pass 2 arguments: $arg1 & $arg2 to the helloworld action of your Users controller:
return $this->redirect(array(
'Users::helloworld',
'args' => array(
$arg1,
$arg2
)
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow