如何在CAKEPHP中訪問GET請求?如何在CAKEPHP中訪問GET請求?
如果我傳遞一個變量在URL
http://samplesite.com/page?key1=value1&key2=value2
我應該使用$ _GET或$這個 - > PARAMS獲得在控制值是多少? CAKEPHP的標準是什麼?
如何在CAKEPHP中訪問GET請求?如何在CAKEPHP中訪問GET請求?
如果我傳遞一個變量在URL
http://samplesite.com/page?key1=value1&key2=value2
我應該使用$ _GET或$這個 - > PARAMS獲得在控制值是多少? CAKEPHP的標準是什麼?
在Cake中執行此操作的標準方法是使用$this->params
。
$value1 = $this->params['url']['key1'];
$value2 = $this->params['url']['key2'];
根據CakePHP的書,「最常見的用法的這 - $> params爲訪問已通過GET或POST操作交給控制器的信息。」
請參閱here。
在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
謝謝。第一篇文章有時並沒有提到,但第二篇文章是:D – func0der 2013-05-11 13:54:40
甚至是$ this-> request-> query('page');而不是$ this-> request-> query ['page']; – antongorodezkiy 2014-01-25 18:00:42
是否需要:if($ this-> request-> is('get')){$ this-> request-> query ['page'];} – 2015-01-02 03:25:07
而現在,我們有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
你可以做到這一點只拿到網址參數,
$this->request->pass; //Array of all parameters in URL
謝謝老兄......難道我避免使用$ _GET在cakephp? – AnNaMaLaI 2011-05-26 06:37:14
@ cakephp.saint是的,當我在Cake工作時,我通常不直接訪問$ _GET或$ _POST。我只是通過手冊的鏈接更新了我的答案。 – declan 2011-05-26 06:44:51