mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #593 from willrossi/trac3881
API: add $includeGetVars flag for SS_HTTPRequest().
This commit is contained in:
commit
976f1f5da0
@ -228,10 +228,30 @@ class SS_HTTPRequest implements ArrayAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Returns the URL used to generate the page
|
||||||
|
*
|
||||||
|
* @param bool $includeGetVars whether or not to include the get parameters\
|
||||||
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function getURL() {
|
function getURL($includeGetVars = false) {
|
||||||
return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url;
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,4 +242,16 @@ class HTTPRequestTest extends SapphireTest {
|
|||||||
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
|
$req->addHeader('X-Requested-With', 'XMLHttpRequest');
|
||||||
$this->assertTrue($req->isAjax());
|
$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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user