Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract URL value with CakePHP (params)

I know that CakePHP params easily extracts values from an URL like this one:

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

I need to extract values from an URL like this:

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

I only need the value from this id:

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:

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

Any ideas guys?

like image 421
learner23 Avatar asked Oct 24 '12 09:10

learner23


2 Answers

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

like image 179
GBD Avatar answered Oct 16 '22 17:10

GBD


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:

$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

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

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

$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):

if ($this->request->is(post)) {
    //validate and save here
} else {
    $this->request->data['Listing']['vt_tour'] = $id;
}
like image 38
mark Avatar answered Oct 16 '22 18:10

mark