Browse Source

Merge pull request #8362 from cakephp/proxies-to-assetUrl

Added proxies to `UrlHelper::assetUrl()`
Mark Story 10 years ago
parent
commit
c4873c8d72

+ 3 - 3
src/View/Helper/HtmlHelper.php

@@ -437,7 +437,7 @@ class HtmlHelper extends Helper
         if (strpos($path, '//') !== false) {
             $url = $path;
         } else {
-            $url = $this->Url->assetUrl($path, $options + ['pathPrefix' => Configure::read('App.cssBaseUrl'), 'ext' => '.css']);
+            $url = $this->Url->css($path, $options);
             $options = array_diff_key($options, ['fullBase' => null, 'pathPrefix' => null]);
         }
 
@@ -528,7 +528,7 @@ class HtmlHelper extends Helper
         }
 
         if (strpos($url, '//') === false) {
-            $url = $this->Url->assetUrl($url, $options + ['pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js']);
+            $url = $this->Url->script($url, $options);
             $options = array_diff_key($options, ['fullBase' => null, 'pathPrefix' => null]);
         }
         if ($options['once'] && isset($this->_includedAssets[__METHOD__][$url])) {
@@ -808,7 +808,7 @@ class HtmlHelper extends Helper
      */
     public function image($path, array $options = [])
     {
-        $path = $this->Url->assetUrl($path, $options + ['pathPrefix' => Configure::read('App.imageBaseUrl')]);
+        $path = $this->Url->image($path, $options);
         $options = array_diff_key($options, ['fullBase' => null, 'pathPrefix' => null]);
 
         if (!isset($options['alt'])) {

+ 65 - 2
src/View/Helper/UrlHelper.php

@@ -41,8 +41,71 @@ class UrlHelper extends Helper
     }
 
     /**
-     * Generate URL for given asset file. Depending on options passed provides full URL with domain name.
-     * Also calls Helper::assetTimestamp() to add timestamp to local files
+     * Generates URL for given image file.
+     *
+     * Depending on options passed provides full URL with domain name. Also calls
+     * `Helper::assetTimestamp()` to add timestamp to local files.
+     *
+     * @param string|array $path Path string or URL array
+     * @param array $options Options array. Possible keys:
+     *   `fullBase` Return full URL with domain name
+     *   `pathPrefix` Path prefix for relative URLs
+     *   `plugin` False value will prevent parsing path as a plugin
+     * @return string Generated URL
+     */
+    public function image($path, array $options = [])
+    {
+        $pathPrefix = Configure::read('App.imageBaseUrl');
+        return $this->assetUrl($path, $options + compact('pathPrefix'));
+    }
+
+    /**
+     * Generates URL for given CSS file.
+     *
+     * Depending on options passed provides full URL with domain name. Also calls
+     * `Helper::assetTimestamp()` to add timestamp to local files.
+     *
+     * @param string|array $path Path string or URL array
+     * @param array $options Options array. Possible keys:
+     *   `fullBase` Return full URL with domain name
+     *   `pathPrefix` Path prefix for relative URLs
+     *   `ext` Asset extension to append
+     *   `plugin` False value will prevent parsing path as a plugin
+     * @return string Generated URL
+     */
+    public function css($path, array $options = [])
+    {
+        $pathPrefix = Configure::read('App.cssBaseUrl');
+        $ext = '.css';
+        return $this->assetUrl($path, $options + compact('pathPrefix', 'ext'));
+    }
+
+    /**
+     * Generates URL for given javascript file.
+     *
+     * Depending on options passed provides full URL with domain name. Also calls
+     * `Helper::assetTimestamp()` to add timestamp to local files.
+     *
+     * @param string|array $path Path string or URL array
+     * @param array $options Options array. Possible keys:
+     *   `fullBase` Return full URL with domain name
+     *   `pathPrefix` Path prefix for relative URLs
+     *   `ext` Asset extension to append
+     *   `plugin` False value will prevent parsing path as a plugin
+     * @return string Generated URL
+     */
+    public function script($path, array $options = [])
+    {
+        $pathPrefix = Configure::read('App.jsBaseUrl');
+        $ext = '.js';
+        return $this->assetUrl($path, $options + compact('pathPrefix', 'ext'));
+    }
+
+    /**
+     * Generates URL for given asset file.
+     *
+     * Depending on options passed provides full URL with domain name. Also calls
+     * `Helper::assetTimestamp()` to add timestamp to local files.
      *
      * @param string|array $path Path string or URL array
      * @param array $options Options array. Possible keys:

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

@@ -252,6 +252,61 @@ class UrlHelperTest extends TestCase
     }
 
     /**
+     * test script()
+     *
+     * @return void
+     */
+    public function testScript()
+    {
+        Router::connect('/:controller/:action/*');
+
+        $this->Helper->webroot = '';
+        $result = $this->Helper->script(
+            [
+                'controller' => 'js',
+                'action' => 'post',
+                '_ext' => 'js'
+            ],
+            ['fullBase' => true]
+        );
+        $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
+    }
+
+    /**
+     * test image()
+     *
+     * @return void
+     */
+    public function testImage()
+    {
+        $result = $this->Helper->image('foo.jpg');
+        $this->assertEquals('img/foo.jpg', $result);
+
+        $result = $this->Helper->image('foo.jpg', ['fullBase' => true]);
+        $this->assertEquals(Router::fullBaseUrl() . '/img/foo.jpg', $result);
+
+        $result = $this->Helper->image('dir/sub dir/my image.jpg');
+        $this->assertEquals('img/dir/sub%20dir/my%20image.jpg', $result);
+
+        $result = $this->Helper->image('foo.jpg?one=two&three=four');
+        $this->assertEquals('img/foo.jpg?one=two&three=four', $result);
+
+        $result = $this->Helper->image('dir/big+tall/image.jpg');
+        $this->assertEquals('img/dir/big%2Btall/image.jpg', $result);
+    }
+
+    /**
+     * test css
+     *
+     * @return void
+     */
+    public function testCss()
+    {
+        $result = $this->Helper->css('style');
+        $this->assertEquals('css/style.css', $result);
+    }
+
+    /**
      * Test generating paths with webroot().
      *
      * @return void