Skip to content

Commit

Permalink
API: add $includeGetVars flag for SS_HTTPRequest() to return the URL …
Browse files Browse the repository at this point in the history
…with the attached GET parameters.
  • Loading branch information
wilr committed Jun 29, 2012
1 parent 1686636 commit 16cb504
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
24 changes: 22 additions & 2 deletions control/HTTPRequest.php
Expand Up @@ -228,10 +228,30 @@ function removeHeader($header) {
}

/**
* Returns the URL used to generate the page
*
* @param bool $includeGetVars whether or not to include the get parameters\
*
* @return string
*/
function getURL() {
return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;
function getURL($includeGetVars = false) {
$url = ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;

if ($includeGetVars) {
// if we don't unset $vars['url'] we end up with /my/url?url=my/url&foo=bar etc

$vars = $this->getVars();
unset($vars['url']);

if (count($vars)) {
$url .= '?' . http_build_query($vars);
}
}
else if(strpos($url, "?") !== false) {
$url = substr($url, 0, strpos($url, "?"));
}

return $url;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/control/HTTPRequestTest.php
Expand Up @@ -242,4 +242,16 @@ function testIsAjax() {
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
$this->assertTrue($req->isAjax());
}

public function testGetURL() {
$req = new SS_HTTPRequest('GET', '/');
$this->assertEquals('', $req->getURL());

$req = new SS_HTTPRequest('GET', '/assets/somefile.gif');
$this->assertEquals('assets/somefile.gif', $req->getURL());

$req = new SS_HTTPRequest('GET', '/home?test=1');
$this->assertEquals('home?test=1', $req->getURL(true));
$this->assertEquals('home', $req->getURL());
}
}

0 comments on commit 16cb504

Please sign in to comment.