Skip to content
This repository
Browse code

APICHANGE: Changed behaviour of HTTP_Request::params to include route…

… table params (as per 2.4 behaviour, see FIX: below).

ADDED: HTTP_Request::params() to retrieve all (shifted) params used in the request
FIXED: Issue where route-table level arguments would not be accessible without using non-deprecated API.
ADDED: Test case to test the above items
UPDATED: Extended Director::test to allow for the retrieval of the request object
UPDATED: Deprecated notice on Director::urlParam and Director::urlParams
REMOVED: Unused variable
FIXED: Coding convention conformity
  • Loading branch information...
commit c2a8eec43c91995c89701ae2e55816458ae01486 1 parent 0aa2894
Damian Mooyman authored August 27, 2012
18  control/Director.php
@@ -167,12 +167,13 @@ static function direct($url, DataModel $model) {
167 167
 	 * @param string $body The HTTP body
168 168
 	 * @param array $headers HTTP headers with key-value pairs
169 169
 	 * @param array $cookies to populate $_COOKIE
  170
+	 * @param HTTP_Request $request The {@see HTTP_Request} object generated as a part of this request
170 171
 	 * @return SS_HTTPResponse
171 172
 	 * 
172 173
 	 * @uses getControllerForURL() The rule-lookup logic is handled by this.
173 174
 	 * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
174 175
 	 */
175  
-	static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null, $cookies = null) {
  176
+	static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null, $cookies = null, &$request = null) {
176 177
 		// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
177 178
 		// Really, it's some inappropriate coupling and should be resolved by making less use of statics
178 179
 		$oldStage = Versioned::current_stage();
@@ -217,10 +218,10 @@ static function test($url, $postVars = null, $session = null, $httpMethod = null
217 218
 		$_COOKIE = (array) $cookies;
218 219
 		$_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
219 220
 
220  
-		$req = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
221  
-		if($headers) foreach($headers as $k => $v) $req->addHeader($k, $v);
  221
+		$request = new SS_HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
  222
+		if($headers) foreach($headers as $k => $v) $request->addHeader($k, $v);
222 223
 		// TODO: Pass in the DataModel
223  
-		$result = Director::handleRequest($req, $session, DataModel::inst());
  224
+		$result = Director::handleRequest($request, $session, DataModel::inst());
224 225
 		
225 226
 		// Restore the superglobals
226 227
 		$_REQUEST = $existingRequestVars; 
@@ -257,6 +258,7 @@ protected static function handleRequest(SS_HTTPRequest $request, Session $sessio
257 258
 			}
258 259
 
259 260
 			if(($arguments = $request->match($pattern, true)) !== false) {
  261
+				$request->setRouteParams($controllerOptions);
260 262
 				// controllerOptions provide some default arguments
261 263
 				$arguments = array_merge($controllerOptions, $arguments);
262 264
 
@@ -294,20 +296,20 @@ protected static function handleRequest(SS_HTTPRequest $request, Session $sessio
294 296
 	/**
295 297
 	 * Returns the urlParam with the given name
296 298
 	 * 
297  
-	 * @deprecated 3.0 Use SS_HTTPRequest->latestParam()
  299
+	 * @deprecated 3.0 Use SS_HTTPRequest->param()
298 300
 	 */
299 301
 	static function urlParam($name) {
300  
-		Deprecation::notice('3.0', 'Use SS_HTTPRequest->latestParam() instead.');
  302
+		Deprecation::notice('3.0', 'Use SS_HTTPRequest->param() instead.');
301 303
 		if(isset(Director::$urlParams[$name])) return Director::$urlParams[$name];
302 304
 	}
303 305
 	
304 306
 	/**
305 307
 	 * Returns an array of urlParams.
306 308
 	 * 
307  
-	 * @deprecated 3.0 Use SS_HTTPRequest->latestParams()
  309
+	 * @deprecated 3.0 Use SS_HTTPRequest->params()
308 310
 	 */
309 311
 	static function urlParams() {
310  
-		Deprecation::notice('3.0', 'Use SS_HTTPRequest->latestParams() instead.');
  312
+		Deprecation::notice('3.0', 'Use SS_HTTPRequest->params() instead.');
311 313
 		return Director::$urlParams;
312 314
 	}
313 315
 
41  control/HTTPRequest.php
@@ -82,6 +82,21 @@ class SS_HTTPRequest implements ArrayAccess {
82 82
 	 */
83 83
 	protected $latestParams = array();
84 84
 	
  85
+	/**
  86
+	 * @var array $routeParams Contains an associative array of all arguments
  87
+	 * explicitly set in the route table for the current request.
  88
+	 * Useful for passing generic arguments via custom routes.
  89
+	 * 
  90
+	 * E.g. The "Locale" parameter would be assigned "en_NZ" below
  91
+	 * 
  92
+	 * Director:
  93
+	 *   rules:
  94
+	 *     'en_NZ/$URLSegment!//$Action/$ID/$OtherID':
  95
+	 *       Controller: 'ModelAsController'
  96
+	 *       Locale: 'en_NZ'
  97
+	 */
  98
+	protected $routeParams = array();
  99
+	
85 100
 	protected $unshiftedButParsedParts = 0;
86 101
 	
87 102
 	/**
@@ -364,7 +379,6 @@ function match($pattern, $shiftOnSuccess = false) {
364 379
 			$shiftCount = sizeof($patternParts);
365 380
 		}
366 381
 
367  
-		$matched = true;
368 382
 		$arguments = array();
369 383
 		foreach($patternParts as $i => $part) {
370 384
 			$part = trim($part);
@@ -447,22 +461,35 @@ public function shiftAllParams() {
447 461
 	function latestParams() {
448 462
 		return $this->latestParams;
449 463
 	}
  464
+	
450 465
 	function latestParam($name) {
451  
-		if(isset($this->latestParams[$name]))
452  
-			return $this->latestParams[$name];
453  
-		else
454  
-			return null;
  466
+		if(isset($this->latestParams[$name])) return $this->latestParams[$name];
  467
+		else return null;
  468
+	}
  469
+	
  470
+	function routeParams() {
  471
+		return $this->routeParams;
  472
+	}
  473
+	
  474
+	function setRouteParams($params) {
  475
+		$this->routeParams = $params;
  476
+	}
  477
+	
  478
+	function params()
  479
+	{
  480
+		return array_merge($this->allParams, $this->routeParams);
455 481
 	}
456 482
 	
457 483
 	/**
458 484
 	 * Finds a named URL parameter (denoted by "$"-prefix in $url_handlers)
459  
-	 * from the full URL.
  485
+	 * from the full URL, or a parameter specified in the route table
460 486
 	 * 
461 487
 	 * @param string $name
462 488
 	 * @return string Value of the URL parameter (if found)
463 489
 	 */
464 490
 	function param($name) {
465  
-		if(isset($this->allParams[$name])) return $this->allParams[$name];
  491
+		$params = $this->params();
  492
+		if(isset($params[$name])) return $params[$name];
466 493
 		else return null;
467 494
 	}
468 495
 	
27  tests/control/DirectorTest.php
@@ -18,7 +18,11 @@ function setUp() {
18 18
 		}
19 19
 		
20 20
 		Config::inst()->update('Director', 'rules', array(
21  
-			'DirectorTestRule/$Action/$ID/$OtherID' => 'DirectorTestRequest_Controller'
  21
+			'DirectorTestRule/$Action/$ID/$OtherID' => 'DirectorTestRequest_Controller',
  22
+			'en-nz/$Action/$ID/$OtherID' => array(
  23
+				'Controller' => 'DirectorTestRequest_Controller',
  24
+				'Locale' => 'en_NZ'
  25
+			)
22 26
 		));
23 27
 	}
24 28
 	
@@ -204,6 +208,27 @@ function testURLParams() {
204 208
 
205 209
 		Deprecation::restore_settings($originalDeprecation);
206 210
 	}
  211
+	
  212
+	/**
  213
+	 * Tests that additional parameters specified in the routing table are 
  214
+	 * saved in the request 
  215
+	 */
  216
+	function testRouteParams() {
  217
+		Deprecation::notification_version('2.4');
  218
+
  219
+		Director::test('en-nz/myaction/myid/myotherid', null, null, null, null, null, null, $request);
  220
+		
  221
+		$this->assertEquals(
  222
+			$request->params(), 
  223
+			array(
  224
+				'Controller' => 'DirectorTestRequest_Controller',
  225
+				'Action' => 'myaction', 
  226
+				'ID' => 'myid', 
  227
+				'OtherID' => 'myotherid',
  228
+				'Locale' => 'en_NZ'
  229
+			)
  230
+		);
  231
+	}
207 232
 
208 233
 	function testForceSSLProtectsEntireSite() {
209 234
 		$_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';

0 notes on commit c2a8eec

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