From 7c87d36d743e24cc0f13d1903baed128a0a3d73a Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 14 Jan 2009 21:38:11 +0000 Subject: [PATCH] Updating Router::queryString() and Router::url() to allow additional parameters to force escaping of & used in query strings. Tests added. Refs #5982 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7987 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/router.php | 26 ++++++++++++++++++++------ cake/tests/cases/libs/router.test.php | 4 ++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cake/libs/router.php b/cake/libs/router.php index 35587fe04..94ccec4f1 100644 --- a/cake/libs/router.php +++ b/cake/libs/router.php @@ -740,7 +740,10 @@ class Router extends Object { * or an array specifying any of the following: 'controller', 'action', * and/or 'plugin', in addition to named arguments (keyed array elements), * and standard URL arguments (indexed array elements) - * @param boolean $full If true, the full base URL will be prepended to the result + * @param mixed $options If (bool)true, the full base URL will be prepended to the result. + * If an array accepts the following keys + * escape - used when making urls embedded in html escapes query string '&' + * full - if true the full base URL will be prepended. * @return string Full translated URL with base path. * @access public * @static @@ -748,7 +751,13 @@ class Router extends Object { function url($url = null, $full = false) { $_this =& Router::getInstance(); $defaults = $params = array('plugin' => null, 'controller' => null, 'action' => 'index'); - + + if (is_bool($full)) { + $escape = false; + } else { + extract(array_merge(array('escape' => false, 'full' => false), $full)); + } + if (!empty($_this->__params)) { if (isset($this) && !isset($this->params['requested'])) { $params = $_this->__params[0]; @@ -919,7 +928,7 @@ class Router extends Object { $output = substr($output, 0, -1); } - return $output . $extension . $_this->queryString($q) . $frag; + return $output . $extension . $_this->queryString($q, array(), $escape) . $frag; } /** * Maps a URL array onto a route and returns the string result, or false if no match @@ -1140,15 +1149,20 @@ class Router extends Object { * Generates a well-formed querystring from $q * * @param mixed $q Query string - * @param array $extra Extra querystring parameters + * @param array $extra Extra querystring parameters. + * @param bool $escape Whether or not to use escaped & * @return array * @access public * @static */ - function queryString($q, $extra = array()) { + function queryString($q, $extra = array(), $escape = false) { if (empty($q) && empty($extra)) { return null; } + $join = '&'; + if ($escape === true) { + $join = '&'; + } $out = ''; if (is_array($q)) { @@ -1157,7 +1171,7 @@ class Router extends Object { $out = $q; $q = $extra; } - $out .= http_build_query($q, null, '&'); + $out .= http_build_query($q, null, $join); if (isset($out[0]) && $out[0] != '?') { $out = '?' . $out; } diff --git a/cake/tests/cases/libs/router.test.php b/cake/tests/cases/libs/router.test.php index c9b88268f..6df28c491 100644 --- a/cake/tests/cases/libs/router.test.php +++ b/cake/tests/cases/libs/router.test.php @@ -1059,6 +1059,10 @@ class RouterTest extends CakeTestCase { $result = Router::url(array('controller' => 'posts', 'action'=>'index', '0', '?' => array('var' => 'test', 'var2' => 'test2', 'more' => 'test data'))); $this->assertEqual($result, $expected); ini_set('arg_separator.output', $restore); + + $result = Router::url(array('controller' => 'posts', 'action'=>'index', '0', '?' => array('var' => 'test', 'var2' => 'test2')), array('escape' => true)); + $expected = '/posts/index/0?var=test&var2=test2'; + $this->assertEqual($result, $expected); } /** * testConnectNamed method