Extract URL value using CakePHP (params)

I know CakePHP parameters easily extract values โ€‹โ€‹from a URL like this:

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

I need to extract values โ€‹โ€‹from a url like:

 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 regular PHP $ _GET will get this easily, but I canโ€™t insert it into my DB, I used this code:

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

Any ideas guys?

+4
source share
3 answers

Use this method

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

here here on cakephp manual http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Controllers.html#the-parameters-attribute-params

+13
source

You did not specify the version of the cake you are using. please always do this. not to mention this, you will get a lot of false answers, because a lot changes during versions.

if you are using the latest 2.3.0, for example, you can use the recently 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 old ways also work:

 $id = $this->request->params->url['id']; // property access $id = $this->request->params[url]['id']; // array access 

you cannot use named with

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

your url will need to be www.example.com/tester/retrieve_test/good/id:012345 . therefore havelock answer is incorrect

then pass your identifier to the default form - or in your case directly to the save statement after the submitted form (there is no need to use the hidden field here).

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

if you really need / want to submit it to the form, use the else $this->request->is(post) block:

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

Alternatively, you can also use so-called named parameters

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

Source: https://habr.com/ru/post/1441772/


All Articles