Permalink
Browse files

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
  • Loading branch information...
1 parent ba42eb0 commit 7c87d36d743e24cc0f13d1903baed128a0a3d73a @markstory markstory committed Jan 14, 2009
Showing with 24 additions and 6 deletions.
  1. +20 −6 cake/libs/router.php
  2. +4 −0 cake/tests/cases/libs/router.test.php
View
@@ -740,15 +740,24 @@ function promote($which = null) {
* 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
*/
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 @@ function url($url = null, $full = false) {
$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 @@ function matchNamed($param, $val, $rule, $context = array()) {
* 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 @@ function queryString($q, $extra = array()) {
$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;
}
@@ -1059,6 +1059,10 @@ function testQuerystringGeneration() {
$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

0 comments on commit 7c87d36

Please sign in to comment.