Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding Router::reverse and basic test case.

  • Loading branch information...
commit 886cd9e719beae718b4f6aadbcb0f7ffc5eec367 1 parent 1eff024
@markstory markstory authored
Showing with 60 additions and 0 deletions.
  1. +21 −0 cake/libs/router.php
  2. +39 −0 cake/tests/cases/libs/router.test.php
View
21 cake/libs/router.php
@@ -982,6 +982,27 @@ function queryString($q, $extra = array(), $escape = false) {
}
/**
+ * Reverses a parsed parameter array into a string. Works similarily to Router::url(), but
+ * Since parsed URL's contain additional 'pass' and 'named' as well as 'url.url' keys.
+ * Those keys need to be specially handled in order to reverse a params array into a string url.
+ *
+ * @param array $param The params array that needs to be reversed.
+ * @return string The string that is the reversed result of the array
+ * @access public
+ */
+ function reverse($params) {
+ $pass = $params['pass'];
+ $named = $params['named'];
+ $url = $params['url'];
+ unset($params['pass'], $params['named'], $params['url'], $url['url']);
+ $params = array_merge($params, $pass, $named);
+ if (!empty($url)) {
+ $params['q'] = $url;
+ }
+ return Router::url($params);
+ }
+
+/**
* Normalizes a URL for purposes of comparison
*
* @param mixed $url URL to normalize
View
39 cake/tests/cases/libs/router.test.php
@@ -1942,6 +1942,45 @@ function testUsingCustomRouteClass() {
$result = Router::parse('/test');
$this->assertEqual($result, $expected);
}
+
+/**
+ * test reversing parameter arrays back into strings.
+ *
+ * @return void
+ */
+ function testRouterReverse() {
+ $params = array(
+ 'controller' => 'posts',
+ 'action' => 'view',
+ 'pass' => array(1),
+ 'named' => array(),
+ 'url' => array()
+ );
+ $result = Router::reverse($params);
+ $this->assertEqual($result, '/posts/view/1');
+
+ $params = array(
+ 'controller' => 'posts',
+ 'action' => 'index',
+ 'pass' => array(1),
+ 'named' => array('page' => 1, 'sort' => 'Article.title', 'direction' => 'desc'),
+ 'url' => array()
+ );
+ $result = Router::reverse($params);
+ $this->assertEqual($result, '/posts/index/1/page:1/sort:Article.title/direction:desc');
+
+ Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));
+ $params = array(
+ 'lang' => 'eng',
+ 'controller' => 'posts',
+ 'action' => 'view',
+ 'pass' => array(1),
+ 'named' => array(),
+ 'url' => array('url' => 'eng/posts/view/1')
+ );
+ $result = Router::reverse($params);
+ $this->assertEqual($result, '/eng/posts/view/1');
+ }
}
/**

0 comments on commit 886cd9e

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