Skip to content
Permalink
Browse files

Allow unescaped URLs to be built. Other helpers need this internally.

  • Loading branch information...
dereuromark committed Sep 19, 2016
1 parent 1ef3453 commit 77f58c994f5e2876eb07009542b6d150d55f42fe
Showing with 43 additions and 3 deletions.
  1. +23 −3 src/View/Helper/UrlHelper.php
  2. +20 −0 tests/TestCase/View/Helper/UrlHelperTest.php
@@ -29,15 +29,35 @@ class UrlHelper extends Helper
/**
* Returns a URL based on provided parameters.
*
* ### Options:
*
* - `escape`: If false, the URL will be returned unescaped, do only use if it is manually
* escaped afterwards before being displayed.
* - `full`: If true, the full base URL will be prepended to the result
*
* @param string|array|null $url Either a relative string url like `/products/view/23` or
* an array of URL parameters. Using an array for URLs will allow you to leverage
* the reverse routing features of CakePHP.
* @param bool $full If true, the full base URL will be prepended to the result
* @param bool $options Array options; bool $full for BC reasons.
* @return string Full translated URL with base path.
*/
public function build($url = null, $full = false)
public function build($url = null, $options = false)
{
return h(Router::url($url, $full));
$defaults = [
'full' => false,
'escape' => true,
];
if (!is_array($options)) {
$options = ['full' => $options];
}
$options += $defaults;
$url = Router::url($url, $options['full']);
if ($options['escape']) {
$url = h($url);
}
return $url;
}
/**
@@ -99,6 +99,26 @@ public function testUrlConversion()
$this->assertEquals("/posts/index?one=value&two=value&three=purple&page=1", $result);
}
/**
* @return void
*/
public function testUrlConversionUnescaped()
{
$result = $this->Helper->build('/controller/action/1?one=1&two=2', ['escape' => false]);
$this->assertEquals('/controller/action/1?one=1&two=2', $result);
$result = $this->Helper->build([
'controller' => 'posts',
'action' => 'view',
'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24',
'?' => [
'k' => 'v',
'1' => '2'
]
], ['escape' => false]);
$this->assertEquals("/posts/view?k=v&1=2&param=%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
}
/**
* test assetTimestamp application
*

0 comments on commit 77f58c9

Please sign in to comment.
You can’t perform that action at this time.