Browse Source

Merge pull request #9483 from cakephp/master-urls

Allow unescaped URLs to be built. Other helpers need this internally.
Mark Story 9 years ago
parent
commit
0df4c2aede
2 changed files with 43 additions and 3 deletions
  1. 23 3
      src/View/Helper/UrlHelper.php
  2. 20 0
      tests/TestCase/View/Helper/UrlHelperTest.php

+ 23 - 3
src/View/Helper/UrlHelper.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.
+     * - `fullBase`: 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 array|bool $options Array of 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 = [
+            'fullBase' => false,
+            'escape' => true,
+        ];
+        if (!is_array($options)) {
+            $options = ['fullBase' => $options];
+        }
+        $options += $defaults;
+
+        $url = Router::url($url, $options['fullBase']);
+        if ($options['escape']) {
+            $url = h($url);
+        }
+
+        return $url;
     }
 
     /**

+ 20 - 0
tests/TestCase/View/Helper/UrlHelperTest.php

@@ -100,6 +100,26 @@ class UrlHelperTest extends TestCase
     }
 
     /**
+     * @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
      *
      * @return void