Skip to content
This repository
Browse code

Adding Router::reverse and basic test case.

  • Loading branch information...
commit 886cd9e719beae718b4f6aadbcb0f7ffc5eec367 1 parent 1eff024
Mark Story authored December 27, 2009
21  cake/libs/router.php
@@ -982,6 +982,27 @@ function queryString($q, $extra = array(), $escape = false) {
982 982
 	}
983 983
 
984 984
 /**
  985
+ * Reverses a parsed parameter array into a string. Works similarily to Router::url(), but
  986
+ * Since parsed URL's contain additional 'pass' and 'named' as well as 'url.url' keys.
  987
+ * Those keys need to be specially handled in order to reverse a params array into a string url.
  988
+ *
  989
+ * @param array $param The params array that needs to be reversed.
  990
+ * @return string The string that is the reversed result of the array
  991
+ * @access public
  992
+ */
  993
+	function reverse($params) {
  994
+		$pass = $params['pass'];
  995
+		$named = $params['named'];
  996
+		$url = $params['url'];
  997
+		unset($params['pass'], $params['named'], $params['url'], $url['url']);
  998
+		$params = array_merge($params, $pass, $named);
  999
+		if (!empty($url)) {
  1000
+			$params['q'] = $url;
  1001
+		}
  1002
+		return Router::url($params);
  1003
+	}
  1004
+
  1005
+/**
985 1006
  * Normalizes a URL for purposes of comparison
986 1007
  *
987 1008
  * @param mixed $url URL to normalize
39  cake/tests/cases/libs/router.test.php
@@ -1942,6 +1942,45 @@ function testUsingCustomRouteClass() {
1942 1942
 		$result = Router::parse('/test');
1943 1943
 		$this->assertEqual($result, $expected);
1944 1944
 	}
  1945
+
  1946
+/**
  1947
+ * test reversing parameter arrays back into strings.
  1948
+ *
  1949
+ * @return void
  1950
+ */
  1951
+	function testRouterReverse() {
  1952
+		$params = array(
  1953
+			'controller' => 'posts',
  1954
+			'action' => 'view',
  1955
+			'pass' => array(1),
  1956
+			'named' => array(),
  1957
+			'url' => array()
  1958
+		);
  1959
+		$result = Router::reverse($params);
  1960
+		$this->assertEqual($result, '/posts/view/1');
  1961
+
  1962
+		$params = array(
  1963
+			'controller' => 'posts',
  1964
+			'action' => 'index',
  1965
+			'pass' => array(1),
  1966
+			'named' => array('page' => 1, 'sort' => 'Article.title', 'direction' => 'desc'),
  1967
+			'url' => array()
  1968
+		);
  1969
+		$result = Router::reverse($params);
  1970
+		$this->assertEqual($result, '/posts/index/1/page:1/sort:Article.title/direction:desc');
  1971
+
  1972
+		Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
  1973
+		$params = array(
  1974
+			'lang' => 'eng',
  1975
+			'controller' => 'posts',
  1976
+			'action' => 'view',
  1977
+			'pass' => array(1),
  1978
+			'named' => array(),
  1979
+			'url' => array('url' => 'eng/posts/view/1')
  1980
+		);
  1981
+		$result = Router::reverse($params);
  1982
+		$this->assertEqual($result, '/eng/posts/view/1');
  1983
+	}
1945 1984
 }
1946 1985
 
1947 1986
 /**

0 notes on commit 886cd9e

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