0

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?

coder
  • 23
  • 8
  • Where are you seeing `username` in the request? Besides the REQUEST_URI – Peter Bowers Apr 28 '15 at 18:33
  • Hi @Peter Bowers, I can see it only on REQUEST_URI. As I'm using security component, I tried to use $this->Security->config('unlockedActions', 'checkusername'); but still not working – coder Apr 28 '15 at 18:37
  • By default the request objects `query` property is fed directly with `$_GET` + possible additional parameters extracted from `Request::_url()`, so if there's nothing in the request, then maybe `$_GET` is empty (for whatever reason), or the property is being emptied at some point, or... So, it's time for more debugging. – ndm Apr 28 '15 at 18:50
  • I think cake 3 handles named queries differently than how you are doing it. More like `users/checkusername/username/e` or something like that. – Peter Bowers Apr 28 '15 at 18:58
  • I think it could be something to do with your Server configuration. Post the content of your `.conf` file here, specially if you are using `NGINX`. – AKKAweb Apr 28 '15 at 21:18
  • debug($this->request->query('username')); – CoolLife Apr 29 '15 at 02:22
  • Hi, I narrow the problem and is with the configuration of the server, something with the rewrite rule. I'm using Hiawatha and this is the rewrite rule UrlToolkit { ToolkitID = cakephp RequestURI exists Return Match .* Rewrite /index.php } – coder May 03 '15 at 14:38

1 Answers1

1

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
}
coder
  • 23
  • 8
  • Praise you man. Piece of cake literally. But I couldn't get the PHP-REST-Framework named "dispatch" (the only one I found to fit in one file and support GET-Url-Parameters), to find my params in $_GET. Although UrlToolkit is way more comprehensable than mod_rewrite, but I couldn't get it right. To the future reader: Especially take care of the order of rules. If you would put the 1st match row below the 2nd match, it wouldn't work. Coder: Une pointe. – JackLeEmmerdeur Dec 09 '15 at 16:14