如何在CAKEPHP中访问GET请求?

lp0sw83n  于 2022-11-12  发布在  PHP
关注(0)|答案(6)|浏览(176)

如何在CAKEPHP中访问GET请求?
如果在url中传递变量

http://samplesite.com/page?key1=value1&key2=value2

我应该使用$_GET或$this-〉params来获取控制器中的值吗?CAKEPHP中的标准是什么?

ulydmbyx

ulydmbyx1#

在CakePHP 2.0中,这似乎已经改变了。根据文档,您可以访问$this->request->query$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

brvekthn

brvekthn2#

在Cake中执行此操作的标准方法是使用$this->params

$value1 = $this->params['url']['key1'];
$value2 = $this->params['url']['key2'];

根据CakePHP一书,“$this-〉params最常见的用法是访问通过GET或POST操作传递给控制器的信息。”
看这里。

deikduxw

deikduxw3#

现在我们有了CakePHP 3;您仍然可以在视图中使用$this->request->query('search')
在CakePHP 3.5 +中,您可以使用$this->request->getQuery('search')
http://book.cakephp.org/3.0/en/controllers/request-response.html#request-parameters

vuv7lop3

vuv7lop34#

您只能这样做以获取URL参数,

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

f2uvfpb95#

显示CakePHP文档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');

要从URL访问所有键,您必须使用

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

输出

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

disho6za6#

根据CakePHP 4.0.2
$this-〉请求-〉获取查询()
将以数组的形式给予整个查询字符串
以及用于特定查询
$this-〉请求-〉getQuery(“关键字”)
https://book.cakephp.org/3/en/controllers/request-response.html

相关问题