Passing parameters via redirect

1.5k Views Asked by At

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

2

There are 2 best solutions below

0
greut On BEST ANSWER

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

0
abhshkdz On

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