使用CakePHP提取URL值(PARAMS)(Extract URL value with Cak

2019-08-01 20:23发布

我知道,CakePHP的PARAMS容易从这样一个URL的提取物值:

http://www.example.com/tester/retrieve_test/good/1/accepted/active

我需要从这样的URL中提取值:

http://www.example.com/tester/retrieve_test?status=200&id=1yOhjvRQBgY

我只需要从此ID值:

ID = 1yOhjvRQBgY

我知道,在正常的PHP $ _GET将检索此easally,bhut我不能得到它的值插入到我的数据库,我用这个代码:

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

任何想法家伙?

Answer 1:

用这样的方式

echo $this->params['url']['id'];

它在这里介绍CakePHP手册http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params



Answer 2:

您没有指定要使用的蛋糕版本。 请一直做下去。 不提它会得到你很多错误的答案,因为很多事情的版本中改变。

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

$id = $this->request->query('id'); // clean access using getter method

在您的控制器。 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

你不能使用自命名

$id = $this->request->params['named']['id'] // WRONG

需要您的网址被www.example.com/tester/retrieve_test/good/id:012345 。 所以哈夫洛克的答案不正确

然后就到表单默认通过您的ID - 或你的情况直接提交表单(不需要在这里使用一个隐藏字段)后保存声明。

$this->request->data['Listing']['vt_tour'] = $id;
//save

如果你真的需要/想它传递到窗体,请使用else块$this->request->is(post)

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


Answer 3:

另外,您也可以使用所谓的命名参数

$id = $this->params['named']['id'];


文章来源: Extract URL value with CakePHP (params)