Search code examples
cakephpgetcakephp-3.0hiawatha

cakePHP returning empty parameters on the GET request


I'm having problem to read parameters on the GET request: www.example.com/users/checkusername?username=e

I'm using cakePHP 3, on cakePHP 2.6.4 is working without problem.

By these way I have an empty result:

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

Result:

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

However, checking the whole request it's possible to verify that the parameter is there

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

These is the output:

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] => 
        )

)

Does somebody have any idea how to read this parameter?

After more debugging

I found that the problem is related to the Hiawatha rewrite rule. I just add

<?php echo phpinfo(); ?>

at begining of the file /webroot/index.php.

When I use: https://www.example.com/?code=123

It works and I get these results

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

But when I try: https://www.example.com/users/confirm?code=123

The variables are not showing.

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

My rewrite rule is according recommendation on cakePHP book:

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

Does anyone have the rewrite rule for cakePHP or there is somewhere else to define it?


Solution

  • At the end is always simple

    This is the rewrite rule for cakePHP on Hiawatha server receive GET request

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