auraphp / Aura.Web

Web controllers and support classes
BSD 2-Clause "Simplified" License
83 stars 22 forks source link

request->url->get(); returning NULL #53

Closed antanova closed 8 years ago

antanova commented 8 years ago

I'm using scotch box Vagrant image. When calling

$request->url->get();

it returns NULL. But when I use

parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

the correct path is returned.

So, I did a var_dump to see if I could see what was happening. Here it is:

object(Aura\Web\Request\Url)#28 (4) {
  ["string":protected]=>
  string(18) "http://example.com"
  ["parts":protected]=>
  array(3) {
    ["scheme"]=>
    string(4) "http"
    ["host"]=>
    string(11) "example.com"
    [1]=>
    NULL
  }
  ["secure":protected]=>
  bool(false)
  ["keys":protected]=>
  array(8) {
    [0]=>
    string(6) "scheme"
    [1]=>
    string(4) "host"
    [2]=>
    string(4) "port"
    [3]=>
    string(4) "user"
    [4]=>
    string(4) "pass"
    [5]=>
    string(4) "path"
    [6]=>
    string(5) "query"
    [7]=>
    string(8) "fragment"
  }
}

It seems that example.com is coming from Url::getHostPort(), and that all the components of the request are empty.

I'm instantiating the request like this

$webfactory = new WebFactory($GLOBALS);
$request = $webfactory->newRequest();

Hopefully it's not me missing something silly.

harikt commented 8 years ago

Can you see if this https://github.com/auraphp/Aura.Web/issues/28#issuecomment-38952434 helps.

antanova commented 8 years ago

Thank you! There is a problem with that comment though - as written on the PHP docs

Note: Gotcha Because variable variables may not be used with PHP's Superglobal arrays within functions, the Superglobal arrays may not be passed into compact().

However after reading that thread and seeing what the problem is, if I assign $_SERVER to a variable first, then $GLOBALS seems to work, e.g.

$redundant = $_SERVER;
$webfactory = new WebFactory($GLOBALS);
$request = $webfactory->newRequest();

$path = $request->url->get(PHP_URL_PATH);

gives me my path instead of NULL.

antanova commented 8 years ago

... or, indeed following the subsequent example on the docs works - I had not seen that for some reason.

Thanks again.

harikt commented 8 years ago

cool :) .

Enjoy.