2010-05-11 54 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]);

希望它能帮助你。