Browse Source

Merge pull request #9633 from andtxr/issue-9631

Adding $params argument to prefix() method of RouteBuilder.
Mark Story 9 years ago
parent
commit
9004f6b2ef
2 changed files with 32 additions and 4 deletions
  1. 13 3
      src/Routing/RouteBuilder.php
  2. 19 1
      tests/TestCase/Routing/RouteBuilderTest.php

+ 13 - 3
src/Routing/RouteBuilder.php

@@ -596,17 +596,27 @@ class RouteBuilder
      * to the `Controller\Admin\Api\` namespace.
      *
      * @param string $name The prefix name to use.
-     * @param callable $callback The callback to invoke that builds the prefixed routes.
+     * @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.
+     * @param callable|null $callback The callback to invoke that builds the prefixed routes.
      * @return void
+     * @throws \InvalidArgumentException If a valid callback is not passed
      */
-    public function prefix($name, callable $callback)
+    public function prefix($name, $params = [], callable $callback = null)
     {
+        if ($callback === null) {
+            if (!is_callable($params)) {
+                throw new InvalidArgumentException('A valid callback is expected');
+            }
+            $callback = $params;
+            $params = [];
+        }
         $name = Inflector::underscore($name);
         $path = '/' . $name;
         if (isset($this->_params['prefix'])) {
             $name = $this->_params['prefix'] . '/' . $name;
         }
-        $params = ['prefix' => $name];
+        $params = array_merge($params, ['prefix' => $name]);
         $this->scope($path, $params, $callback);
     }
 

+ 19 - 1
tests/TestCase/Routing/RouteBuilderTest.php

@@ -306,6 +306,23 @@ class RouteBuilderTest extends TestCase
     public function testPrefix()
     {
         $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
+        $res = $routes->prefix('admin', ['param' => 'value'], function ($r) {
+            $this->assertInstanceOf('Cake\Routing\RouteBuilder', $r);
+            $this->assertCount(0, $this->collection->routes());
+            $this->assertEquals('/path/admin', $r->path());
+            $this->assertEquals(['prefix' => 'admin', 'key' => 'value', 'param' => 'value'], $r->params());
+        });
+        $this->assertNull($res);
+    }
+
+    /**
+     * Test creating sub-scopes with prefix()
+     *
+     * @return void
+     */
+    public function testPrefixWithNoParams()
+    {
+        $routes = new RouteBuilder($this->collection, '/path', ['key' => 'value']);
         $res = $routes->prefix('admin', function ($r) {
             $this->assertInstanceOf('Cake\Routing\RouteBuilder', $r);
             $this->assertCount(0, $this->collection->routes());
@@ -323,9 +340,10 @@ class RouteBuilderTest extends TestCase
     public function testNestedPrefix()
     {
         $routes = new RouteBuilder($this->collection, '/admin', ['prefix' => 'admin']);
-        $res = $routes->prefix('api', function ($r) {
+        $res = $routes->prefix('api', ['_namePrefix' => 'api:'], function ($r) {
             $this->assertEquals('/admin/api', $r->path());
             $this->assertEquals(['prefix' => 'admin/api'], $r->params());
+            $this->assertEquals('api:', $r->namePrefix());
         });
         $this->assertNull($res);
     }