Cake PHP 重定向在 url 中带有参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2814777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:45:25  来源:igfitidea点击:

Cake PHP redirect with parameters in url

phpurlcakephpredirectparameters

提问by megaboss98

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

我有一个页面,我想重定向到它需要 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:

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

 $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?

如何使用上述函数添加我想要的参数作为 url 的一部分?

I would think that there might be another element that I could add to array so that I can pass along param1:val1and param2:val2.

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

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by megaboss98

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!)

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

To redirect to the URL:

要重定向到 URL:

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

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);
$this->redirect(array("controller" => "myController", 
                      "action" => "myAction",
                      "param1" => "val1",
                      "param2" => "val2",
                      $data_can_be_passed_here),
                $status,
                $exit);

Hope it helps!

希望能帮助到你!

回答by Serge S.

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

如果您需要使用精确的 get 参数重定向,则将'?'索引传递给$url数组参数:

$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

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

This is true at least in CakePHP 1.3

至少在 CakePHP 1.3 中是这样

回答by Kiran

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/');

?>

回答by D.Dimitrioglo

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

我通常做这样的事情:$this->redirect(['action' => 'view', $id, 'admins' => true]);

Hope it will help you.

希望它会帮助你。