2010-05-11 51 views
24

我有,我想重定向到一個頁面需要的URL參數: http://www.example.com/myController/myAction/param1:val1/param2:val2蛋糕PHP與參數重定向URL

我知道有一個CakePHP的重定向功能重定向的作品如下:

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

如何使用上述函數添加我想要的參數作爲URL的一部分?

我想可能會有另一個元素,我可以添加到數組,以便我可以傳遞param1:val1param2:val2

任何幫助將不勝感激!

回答

48

我不知道爲什麼我無法在CakePHP文檔中找到它,但我終於弄清楚瞭解決方案。如果其他人遇到同樣的問題,我會在此發佈。 (如果有人知道這是在文檔中請張貼還有,謝謝!)

重定向到URL:

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

您可以使用:

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

希望它有幫助!

+0

好的工作。如果它不在文檔中,您可以自己註冊並將其添加到CookBook中,以便其他人可以受益。 – webbiedave 2010-05-12 03:05:28

+2

當一個Cake方法接受一個URL並且傳入一個數組時,'Router :: url()'用於獲取鏈接的字符串表示形式(http://api.cakephp.org/class/router#method -Routerurl)。您可以通過簡單地將它們合併到您傳入的URL數組中(例如,'$ this-> redirect(array_merge(array('controller'=> ...),$ this-> passedArgs)來在整個重定向或鏈接中保留Cake的命名參數))') – deizel 2010-05-12 12:01:16

3

相反,你可以使用這種格式也

<?php 

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


?> 

<?php 

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

?> 
17

如果您需要精確地獲取參數重定向,然後傳遞'?'指數$url數組參數:

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

它重定向到/myController/muAction/...?param1=val1&param2=val2

至少在CakePHP 1.3中這是真的

+0

也適用於CakePHP 2.2.3。 – 2014-03-20 13:05:38

+0

在2.4.6上工作就像一個魅力。謝謝。 – usumoio 2016-02-05 20:57:09

2

我通常會這樣做:$this->redirect(['action' => 'view', $id, 'admins' => true]);

希望它能幫助你。