How can I access a GET request in CAKEPHP ?
If I am passing a variable in the url
http://samplesite.com/page?key1=value1&key2=value2
Should I use $_GET or $this->params to get the values in controller? What is the standard in CAKEPHP ?
To view records of database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using get() method. The get() method will take the name of the database table as argument. Now, this new instance is used to find records from database using find() method.
You can retrieve post data as Array. $post_data= $this->request->data; You can retrieve post data for particular key.
Get Current Url in Cakephp : $this->here is used to get the current url in cakephp. it will give you the absolute current url. $this->request->here is also used to get current url.
Cakephp get url parameters Syntax – // Get the Passed arguments $this->request->pass; $this->request['pass']; $this->request->params['pass']; The above syntax will give you the arguments of url passed along with controller.
In CakePHP 2.0 this appears to have changed. According to the documentation you can access $this->request->query
or $this->request['url']
.
// url is /posts/index?page=1&sort=title $this->request->query['page']; // You can also access it via array access $this->request['url']['page'];
http://book.cakephp.org/2.0/en/controllers/request-response.html
The standard way to do this in Cake is to use $this->params
.
$value1 = $this->params['url']['key1']; $value2 = $this->params['url']['key2'];
According to the CakePHP book, "the most common use of $this->params is to access information that has been handed to the controller via GET or POST operations."
See here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With