php 使用 CakePHP 提取 URL 值(参数)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13046510/
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
Extract URL value with CakePHP (params)
提问by learner23
I know that CakePHP params easily extracts values from an URL like this one:
我知道 CakePHP params 可以轻松地从这样的 URL 中提取值:
http://www.example.com/tester/retrieve_test/good/1/accepted/active
I need to extract values from an URL like this:
我需要从这样的 URL 中提取值:
http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY
I only need the value from this id:
我只需要这个 id 的值:
id=1yOhjvRQBgY
id=1yOhjvRQBgY
I know that in normal PHP $_GET will retrieve this easally, bhut I cant get it to insert the value into my DB, i used this code:
我知道在正常的 PHP 中 $_GET 会很容易地检索到这个,但是我不能让它将值插入到我的数据库中,我使用了这个代码:
$html->input('Listing/vt_tour', array('value'=>$_GET["id"], 'type'=>'hidden'))
Any ideas guys?
有什么想法吗?
回答by GBD
Use this way
用这种方式
echo $this->params['url']['id'];
it's here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
这是在 cakephp 手册http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params
回答by mark
You didn't specify the cake version you are using. please always do so. not mentioning it will get you lots of false answers because lots of things change during versions.
您没有指定您使用的蛋糕版本。请始终这样做。不提它会得到很多错误的答案,因为在版本中很多事情都会发生变化。
if you are using the latest 2.3.0 for example you can use the newly added query method:
例如,如果您使用的是最新的 2.3.0,则可以使用新添加的查询方法:
$id = $this->request->query('id'); // clean access using getter method
in your controller. http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
在您的控制器中。 http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::query
but the old ways also work:
但旧的方法也有效:
$id = $this->request->params->url['id']; // property access
$id = $this->request->params[url]['id']; // array access
you cannot use named since
你不能使用命名因为
$id = $this->request->params['named']['id'] // WRONG
would require your url to be www.example.com/tester/retrieve_test/good/id:012345.
so the answer of havelock is incorrect
将要求您的网址为www.example.com/tester/retrieve_test/good/id:012345. 所以havelock的答案是不正确的
then pass your id on to the form defaults - or in your case directly to the save statement after the form submitted (no need to use a hidden field here).
然后将您的 id 传递给表单默认值 - 或者在您的情况下直接传递给表单提交后的 save 语句(无需在此处使用隐藏字段)。
$this->request->data['Listing']['vt_tour'] = $id;
//save
if you really need/want to pass it on to the form, use the else block of $this->request->is(post):
如果您真的需要/想要将其传递给表单,请使用 else 块$this->request->is(post):
if ($this->request->is(post)) {
//validate and save here
} else {
$this->request->data['Listing']['vt_tour'] = $id;
}
回答by Havelock
Alternatively you could also use the so called named parameters
或者,您也可以使用所谓的命名参数
$id = $this->params['named']['id'];

