使用CakePHP(params)提取URL值

lea*_*r23 5 php mysql url cakephp

我知道CakePHP参数可以轻松地从这样的URL中提取值:

http://www.example.com/tester/retrieve_test/good/1/accepted/active
Run Code Online (Sandbox Code Playgroud)

我需要从URL中提取值,如下所示:

http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
Run Code Online (Sandbox Code Playgroud)

我只需要这个id的值:

ID = 1yOhjvRQBgY

我知道在普通的PHP $ _GET中会很容易地检索这个,但是我不能让它将值插入我的数据库中,我使用了这段代码:

$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

mar*_*ark 12

您没有指定正在使用的蛋糕版本.请始终这样做.没有提到它会给你很多错误的答案,因为在版本期间很多东西都会改变.

如果您使用的是最新的2.3.0,则可以使用新添加的查询方法:

$id = $this->request->query('id'); // clean access using getter method
Run Code Online (Sandbox Code Playgroud)

在你的控制器中. http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query

但旧的方式也有效:

$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
Run Code Online (Sandbox Code Playgroud)

你不能使用named

$id = $this->request->params['named']['id'] // WRONG
Run Code Online (Sandbox Code Playgroud)

需要你的网址www.example.com/tester/retrieve_test/good/id:012345.所以哈夫洛克的答案是错误的

然后将你的id传递给表单默认值 - 或者在你的情况下直接传递给表单提交后的save语句(这里不需要使用隐藏字段).

$this->request->data['Listing']['vt_tour'] = $id;
//save
Run Code Online (Sandbox Code Playgroud)

如果你真的需要/想要将它传递给表单,请使用else块$this->request->is(post):

if ($this->request->is(post)) {
    //validate and save here
} else {
    $this->request->data['Listing']['vt_tour'] = $id;
}
Run Code Online (Sandbox Code Playgroud)