Search code examples
phpurl-routinglithium

Passing parameters via redirect


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