How can I access a GET request in CAKEPHP?

拥有回忆 提交于 2019-12-06 18:58:23

问题


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 ?


回答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.




回答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




回答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




回答4:


You can do this only to get URL params,

$this->request->pass;  //Array of all parameters in URL


来源:https://stackoverflow.com/questions/6134246/how-can-i-access-a-get-request-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!