ソースを参照

Merge pull request #17047 from cakephp/4.5-url-function

Add conveninence function for generating URLs.
ADmad 3 年 前
コミット
2ff09a9846
2 ファイル変更51 行追加16 行削除
  1. 40 16
      src/Routing/functions.php
  2. 11 0
      tests/TestCase/Routing/RouterTest.php

+ 40 - 16
src/Routing/functions.php

@@ -14,27 +14,51 @@ declare(strict_types=1);
  * @since         4.1.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
+// phpcs:disable SlevomatCodingStandard.Namespaces.NamespaceDeclaration.DisallowedBracketedSyntax
+namespace {
 
-use Cake\Routing\Router;
+    use Cake\Routing\Router;
 
-if (!function_exists('urlArray')) {
+    if (!function_exists('urlArray')) {
+        /**
+         * Returns an array URL from a route path string.
+         *
+         * @param string $path Route path.
+         * @param array $params An array specifying any additional parameters.
+         *   Can be also any special parameters supported by `Router::url()`.
+         * @return array URL
+         * @see \Cake\Routing\Router::pathUrl()
+         */
+        function urlArray(string $path, array $params = []): array
+        {
+            $url = Router::parseRoutePath($path);
+            $url += [
+                'plugin' => false,
+                'prefix' => false,
+            ];
+
+            return $url + $params;
+        }
+    }
+}
+
+namespace Cake\Routing {
     /**
-     * Returns an array URL from a route path string.
+     * Convenience wrapper for Router::url().
      *
-     * @param string $path Route path.
-     * @param array $params An array specifying any additional parameters.
-     *   Can be also any special parameters supported by `Router::url()`.
-     * @return array URL
-     * @see \Cake\Routing\Router::pathUrl()
+     * @param \Psr\Http\Message\UriInterface|array|string|null $url An array specifying any of the following:
+     *   'controller', 'action', 'plugin' additionally, you can provide routed
+     *   elements or query string parameters. If string it can be name any valid url
+     *   string or it can be an UriInterface instance.
+     * @param bool $full If true, the full base URL will be prepended to the result.
+     *   Default is false.
+     * @return string Full translated URL with base path.
+     * @throws \Cake\Core\Exception\CakeException When the route name is not found
+     * @see \Cake\Routing\Router::url()
+     * @since 4.5.0
      */
-    function urlArray(string $path, array $params = []): array
+    function url($url = null, bool $full = false): string
     {
-        $url = Router::parseRoutePath($path);
-        $url += [
-            'plugin' => false,
-            'prefix' => false,
-        ];
-
-        return $url + $params;
+        return Router::url($url, $full);
     }
 }

+ 11 - 0
tests/TestCase/Routing/RouterTest.php

@@ -29,6 +29,7 @@ use Cake\TestSuite\TestCase;
 use Exception;
 use InvalidArgumentException;
 use RuntimeException;
+use function Cake\Routing\url;
 
 /**
  * RouterTest class
@@ -3327,6 +3328,16 @@ class RouterTest extends TestCase
     }
 
     /**
+     * Test the url() function which wraps Router::url()
+     *
+     * @return void
+     */
+    public function testUrlFunction(): void
+    {
+        $this->assertSame(Router::url('/'), url('/'));
+    }
+
+    /**
      * Connect some fallback routes for testing router behavior.
      */
     protected function _connectDefaultRoutes(): void