views:

725

answers:

1

I have a page that I want to redirect to that requires parameters in the URL: http://www.mysite.com/myController/myAction/param1:val1/param2:val2

I know that there is a Cake PHP redirect function for redirecting that works as follows:

$this->redirect(array("controller" => "myController", 
                      "action" => "myAction", 
                      $data_can_be_passed_here),
                $status,
                $exit);

How do I add the parameters that I want as part of the url using the above function? I would think that there might be another element that I could add to array so that I can pass along "param1:val1" and "param2:val2". Any help would be greatly appreciated!

+5  A: 

I do not know why I was not able to find this in the CakePHP documentation, but I did finally figure out the solution. I am posting it here in case anyone else has the same problem. (If someone knows where this is in the documentation please post it as well, thanks!)

To redirect to the URL:

http://www.mysite.com/myController/myAction/param1:val1/param2:val2

You can use:

$this->redirect(array("controller" => "myController", 
                      "action" => "myAction",
                      "param1" => "val1",
                      "param2" => "val2",
                      $data_can_be_passed_here),
                $status,
                $exit);

Hope it helps!

megaboss98
Good work. If it's not in the docs, you can sign up and add it to The CookBook yourself so others may benefit.
webbiedave
When a Cake method accepts a URL and you pass in an array instead, `Router::url()` is used to get the string representation of the link (http://api.cakephp.org/class/router#method-Routerurl). You can persist Cake's named parameters across redirects or links by simply merging them into the URL array you pass in (eg. `$this->redirect(array_merge(array('controller' => ...), $this->passedArgs))`)
deizel