Browse Source

Merge pull request #10690 from cakephp/issue-10678

Add path option to RouteBuilder::prefix()
Mark Story 8 years ago
parent
commit
397779599f
2 changed files with 37 additions and 1 deletions
  1. 16 1
      src/Routing/RouteBuilder.php
  2. 21 0
      tests/TestCase/Routing/RouteBuilderTest.php

+ 16 - 1
src/Routing/RouteBuilder.php

@@ -587,7 +587,7 @@ class RouteBuilder
      * This method creates a scoped route collection that includes
      * relevant prefix information.
      *
-     * The path parameter is used to generate the routing parameter name.
+     * The $name parameter is used to generate the routing parameter name.
      * For example a path of `admin` would result in `'prefix' => 'admin'` being
      * applied to all connected routes.
      *
@@ -595,6 +595,17 @@ class RouteBuilder
      * Nested prefixes will result in prefix values like `admin/api` which translates
      * to the `Controller\Admin\Api\` namespace.
      *
+     * If you need to have prefix with dots, eg: '/api/v1.0', use 'path' key
+     * for $params argument:
+     *
+     * ```
+     * $route->prefix('api', function($route) {
+     *     $route->prefix('v10', ['path' => '/v1.0'], function($route) {
+     *         // Translates to `Controller\Api\V10\` namespace
+     *     });
+     * });
+     * ```
+     *
      * @param string $name The prefix name to use.
      * @param array|callable $params An array of routing defaults to add to each connected route.
      *   If you have no parameters, this argument can be a callable.
@@ -613,6 +624,10 @@ class RouteBuilder
         }
         $name = Inflector::underscore($name);
         $path = '/' . $name;
+        if (isset($params['path'])) {
+            $path = $params['path'];
+            unset($params['path']);
+        }
         if (isset($this->_params['prefix'])) {
             $name = $this->_params['prefix'] . '/' . $name;
         }

+ 21 - 0
tests/TestCase/Routing/RouteBuilderTest.php

@@ -351,6 +351,27 @@ class RouteBuilderTest extends TestCase
     }
 
     /**
+     * Test creating sub-scopes with prefix()
+     *
+     * @return void
+     */
+    public function testPathWithDotInPrefix()
+    {
+        $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
+        $res = $routes->prefix('api', function ($r) {
+            $r->prefix('v10', ['path' => '/v1.0'], function ($r2) {
+                $this->assertEquals('/admin/api/v1.0', $r2->path());
+                $this->assertEquals(['prefix' => 'admin/api/v10'], $r2->params());
+                $r2->prefix('b1', ['path' => '/beta.1'], function ($r3) {
+                    $this->assertEquals('/admin/api/v1.0/beta.1', $r3->path());
+                    $this->assertEquals(['prefix' => 'admin/api/v10/b1'], $r3->params());
+                });
+            });
+        });
+        $this->assertNull($res);
+    }
+
+    /**
      * Test creating sub-scopes with plugin()
      *
      * @return void