使用url中的参数对PHP进行重定向

meg*_*s98 29 php parameters url redirect cakephp

我有一个页面,我想重定向到需要URL中的参数:http://www.example.com/myController/myAction/param1 : val1 / param2: val2

我知道有一个用于重定向的CakePHP重定向函数,其工作方式如下:

 $this->redirect(array("controller" => "myController",
                       "action" => "myAction",
                       $data_can_be_passed_here),
                 $status, $exit);
Run Code Online (Sandbox Code Playgroud)

如何使用上述功能添加我想要的参数作为网址的一部分?

我认为可能有另外一个元素,我可以添加到数组,以便我可以传递param1:val1param2:val2.

任何帮助将不胜感激!

meg*_*s98 49

我不知道为什么我无法在CakePHP文档中找到它,但我终于找到了解决方案.我在这里发布它,以防其他人有同样的问题.(如果有人知道文档中的位置,请将其发布,谢谢!)

要重定向到网址:

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);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!

  • 当一个Cake方法接受一个URL而你传入一个数组时,`Router :: url()`用于获取链接的字符串表示形式(http://api.cakephp.org/class/router#method- Routerurl).您可以通过简单地将它们合并到您传入的URL数组中来保持Cake的命名参数(例如`$ this-> redirect(array_merge(array('controller'=> ...),$ this-> passedArgs) ))`) (2认同)

Ser*_* S. 21

如果需要使用完全get参数重定向,则将'?'index 传递给$urlarray参数:

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

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

至少在CakePHP 1.3中是这样


Kir*_*ran 5

相反,您也可以使用这种格式

<?php

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


?>

<?php

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

?>
Run Code Online (Sandbox Code Playgroud)