30

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

I know that there is a CakePHP 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!

semperos
  • 4,674
  • 28
  • 31
megaboss98
  • 911
  • 1
  • 8
  • 9

5 Answers5

50

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.example.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!

Cœur
  • 37,241
  • 25
  • 195
  • 267
megaboss98
  • 911
  • 1
  • 8
  • 9
  • 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 May 12 '10 at 03:05
  • 2
    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. May 12 '10 at 12:01
21

If you need to redirect with exactly get parameters, then pass '?' index to $url array argument:

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

It redirects to /myController/muAction/...?param1=val1&param2=val2

This is true at least in CakePHP 1.3

Serge S.
  • 4,855
  • 3
  • 42
  • 46
5

Instead, you can use this format also

<?php

$this->redirect('/controller/action/par1:par1/par2:par2/');


?>

<?php

$this->redirect('/controller/action/id/10/name/hello/');

?>
Kiran
  • 919
  • 6
  • 8
2

I usually do something like this:$this->redirect(['action' => 'view', $id, 'admins' => true]);

Hope it will help you.

D.Dimitrioglo
  • 3,413
  • 2
  • 21
  • 41
0

In CakePHP 4.2 it seems that "param" => "val1" doesnt work anymore.

This is the way you go:

return $this->redirect(['controller'=>'mycontroller','action' => 'myview', 'myparameter']);
cursorrux
  • 1,382
  • 4
  • 9
  • 20