2011-05-26 54 views
15

如何在CAKEPHP中访问GET请求?如何在CAKEPHP中访问GET请求?

如果我传递一个变量在URL

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

我应该使用$ _GET或$这个 - > PARAMS获得在控制值是多少? CAKEPHP的标准是什么?

回答

20

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

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

根据CakePHP的书,“最常见的用法的这 - $> params为访问已通过GET或POST操作交给控制器的信息。”

请参阅here

+0

谢谢老兄......难道我避免使用$ _GET在cakephp? – AnNaMaLaI 2011-05-26 06:37:14

+1

@ cakephp.saint是的,当我在Cake工作时,我通常不直接访问$ _GET或$ _POST。我只是通过手册的链接更新了我的答案。 – declan 2011-05-26 06:44:51

24

在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

+4

谢谢。第一篇文章有​​时并没有提到,但第二篇文章是:D – func0der 2013-05-11 13:54:40

+3

甚至是$ this-> request-> query('page');而不是$ this-> request-> query ['page']; – antongorodezkiy 2014-01-25 18:00:42

+0

是否需要:if($ this-> request-> is('get')){$ this-> request-> query ['page'];} – 2015-01-02 03:25:07

0

你可以做到这一点只拿到网址参数,

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