2015-04-28 30 views
0

返回空參數: www.example.com/users/checkusername?username=eCakePHP的上我有問題以讀取關於GET請求參數的GET請求

我使用CakePHP 3,上cakePHP 2.6.4正在工作沒有問題。

通過這些方式,我有一個空的結果:

$this->log($this->request->query['username']); 
$this->log($this->params['url']); 

結果:

2015-04-28 17:28:22 Error: 
2015-04-28 17:28:22 Error: 

然而,檢查整個請求有可能驗證參數是有

$this->log($this->request); 

這些是輸出:

2015-04-28 17:28:22 Error: Cake\Network\Request Object 
(
    [params] => Array 
     (
      [plugin] => 
      [controller] => Users 
      [action] => checkusername 
      [_ext] => 
      [pass] => Array 
       (
       ) 

      [_Token] => Array 
       (
        [unlockedFields] => Array 
         (
         ) 

       ) 

     ) 

    [data] => Array 
     (
     ) 

    [query] => Array 
     (
     ) 

    [cookies] => Array 
     (
      [CAKEPHP] => u8lgs1cup81331lka3a90jidd4 
     ) 

    [_environment:protected] => Array 
     (
     **removed** 
      [REQUEST_METHOD] => GET 
      [REQUEST_URI] => /users/checkusername?username=e 
      [SCRIPT_NAME] => /index.php 
     **removed** 
     ) 

    [url] => users/checkusername 
    [base] => 
    [webroot] =>/
    [here] => /users/checkusername 
    [trustProxy] => 
    [_detectorCache:protected] => Array 
     (
      [post] => 
      [put] => 
     ) 

    [_input:protected] => 
    [_session:protected] => Cake\Network\Session Object 
     (
      [_engine:protected] => 
      [_started:protected] => 1 
      [_lifetime:protected] => 1440 
      [_isCli:protected] => 
     ) 

) 

有人知道如何讀取這個參數嗎?

更多的調試

後,我發現這個問題是關係到海華沙重寫規則。我只是添加

<?php echo phpinfo(); ?> 

在開始文件/webroot/index.php。

當我使用:https://www.example.com/?code=123

它的工作原理,我得到這些結果

PHP Variables   Variable Value 
_REQUEST["code"]   123 
_GET["code"]    123 
_SERVER["REQUEST_URI"]  /?code=123 
_SERVER["SCRIPT_NAME"]  /index.php 

但是當我嘗試:https://www.example.com/users/confirm?code=123

的變量沒有顯示。

_SERVER["REQUEST_URI"]  /users/confirm/?code=123 
_SERVER["SCRIPT_NAME"]  /index.php 

我重寫規則是根據介紹CakePHP書推薦:

UrlToolkit { 
    ToolkitID = cakephp 
    RequestURI exists Return 
    Match .* Rewrite /index.php 
} 

沒有人有CakePHP的重寫規則或有其他地方定義的?

+0

你是哪裏的要求看'username'?除了REQUEST_URI –

+0

Hi @Peter Bowers,我只能在REQUEST_URI上看到它。當我使用安全組件時,我嘗試使用$ this-> Security-> config('unlockedActions','checkusername');但仍然不起作用 – coder

+0

默認情況下,請求對象'query'屬性直接使用'$ _GET' +可能從'Request :: _ url()'提取的附加參數提供,所以如果請求中沒有任何內容,則可能是$ _GET '是空的(無論出於何種原因),或者財產在某個時候被清空,或者...因此,是時候進行更多的調試了。 – ndm

回答

1

,結束時總是簡單

這是爲CakePHP的重寫規則海華沙服務器上接收GET請求

UrlToolkit { 
    ToolkitID = cakephp 
    RequestURI exists Return 
    Match .*\?(.*) Rewrite /index.php?$1 
    Match .* Rewrite /index.php 
} 
+0

讚美你的男人。字面上的一塊蛋糕。但是我無法獲得名爲「dispatch」的PHP-REST-Framework(我發現它只適合一個文件並支持GET-Url-Parameters),以在$ _GET中查找我的參數。儘管UrlToolkit比mod_rewrite更容易理解,但我無法正確理解。對未來的讀者:特別關注規則的順序。如果你將第一場比賽排在第二場比賽以下,那麼這將不起作用。編碼器:Une pointe。 – user3469861