How can I access a GET request in CAKEPHP?
Solution 1
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.
Solution 2
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
Solution 3
And now that we have CakePHP 3; you can still use $this->request->query('search')
in your views.
And in CakePHP 3.5 + you can use
$this->request->getQuery('search')
http://book.cakephp.org/3.0/en/controllers/request-response.html#request-parameters
AnNaMaLaI
I am a web developer, Worked on various Social community networking websites and E-commerce websites. Knowledge in Zend/Cakephp/Yii/Magento/Joomla/wordpress/Facebook apps/Twitter apps/ Google map Apis/Yelp Apis/ Janrain Apis
Updated on December 09, 2020Comments
-
AnNaMaLaI over 3 years
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 ?
-
AnNaMaLaI almost 13 yearsThanks Dude... Shall i avoid to use $_GET in cakephp?
-
declan almost 13 years@cakephp.saint Yeah, I typically don't access $_GET or $_POST directly when i'm working in Cake. I just updated my answer with a link to the manual.
-
func0der almost 11 yearsThank you. Rescue comes sometimes no in the first, but the second post :D
-
gorodezkiy over 10 yearsOr even $this->request->query('page'); instead of $this->request->query['page'];
-
I Wanna Know over 9 yearsIs it needed: if ($this->request->is('get')){ $this->request->query['page'];}
-
I Wanna Know over 9 yearsOr can I just: $this->request->query['page'];