开发者

How can I access a GET request in CAKEPHP?

开发者 https://www.devze.com 2023-03-08 16:43 出处:网络
How can I access a GET request in CAKEPHP ? If I am passing a variable in the url http://samplesite.com/p开发者_运维问答age?key1=value1&key2=value2

How can I access a GET request in CAKEPHP ?

If I am passing a variable in the url

http://samplesite.com/p开发者_运维问答age?key1=value1&key2=value2

Should I use $_GET or $this->params to get the values in controller? What is the standard in CAKEPHP ?


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.


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


You can do this only to get URL params,

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


According to CakePHP documentation Query String Parameters

// URL is /posts/index?page=1&sort=title
$page = $this->request->getQuery('page');

// Prior to 3.4.0
$page = $this->request->query('page');

To access the all keys from URL You have to use

$data = $this->request->getQuery();
echo "<pre>";print_r($data ); die('MMS');

Output

<pre>Array
(
    [key1] => value
    [key2] => value
    ...........
)


According to CakePHP 4.0.2

$this->request->getQuery()

will give you the whole query strings as an array

and for specific query

$this->request->getQuery('keywords')

https://book.cakephp.org/3/en/controllers/request-response.html

0

精彩评论

暂无评论...
验证码 换一张
取 消