Skip to content
This repository
Browse code

API: add $includeGetVars flag for SS_HTTPRequest() to return the URL …

…with the attached GET parameters.
  • Loading branch information...
commit 16cb504d8e66a16ce6a734dbc77fc567f9284d96 1 parent 1686636
Will Rossiter authored
24  control/HTTPRequest.php
@@ -228,10 +228,30 @@ function removeHeader($header) {
228 228
 	}
229 229
 	
230 230
 	/**
  231
+	 * Returns the URL used to generate the page
  232
+	 *
  233
+	 * @param bool $includeGetVars whether or not to include the get parameters\
  234
+	 * 
231 235
 	 * @return string
232 236
 	 */
233  
-	function getURL() {
234  
-		return ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url; 
  237
+	function getURL($includeGetVars = false) {
  238
+		$url = ($this->getExtension()) ? $this->url . '.' . $this->getExtension() : $this->url; 
  239
+
  240
+		 if ($includeGetVars) { 
  241
+		 	// if we don't unset $vars['url'] we end up with /my/url?url=my/url&foo=bar etc 
  242
+ 			
  243
+ 			$vars = $this->getVars();
  244
+ 			unset($vars['url']);
  245
+
  246
+ 			if (count($vars)) {
  247
+ 				$url .= '?' . http_build_query($vars);
  248
+ 			}
  249
+ 		}
  250
+ 		else if(strpos($url, "?") !== false) {
  251
+ 			$url = substr($url, 0, strpos($url, "?"));
  252
+ 		}
  253
+
  254
+ 		return $url; 
235 255
 	}
236 256
 
237 257
 	/**
12  tests/control/HTTPRequestTest.php
@@ -242,4 +242,16 @@ function testIsAjax() {
242 242
 		$req->addHeader('X-Requested-With', 'XMLHttpRequest');
243 243
 		$this->assertTrue($req->isAjax());
244 244
 	}
  245
+
  246
+	public function testGetURL() {
  247
+		$req = new SS_HTTPRequest('GET', '/');
  248
+		$this->assertEquals('', $req->getURL());
  249
+
  250
+		$req = new SS_HTTPRequest('GET', '/assets/somefile.gif');
  251
+		$this->assertEquals('assets/somefile.gif', $req->getURL());
  252
+
  253
+		$req = new SS_HTTPRequest('GET', '/home?test=1');
  254
+		$this->assertEquals('home?test=1', $req->getURL(true));
  255
+		$this->assertEquals('home', $req->getURL());
  256
+	}
245 257
 }

0 notes on commit 16cb504

Please sign in to comment.
Something went wrong with that request. Please try again.