Browse Source

Merge pull request #15414 from cakephp/backport-15395

Backport: Fix RouteBuilder::plugin() not forwarding namePrefix option
Mark Story 5 years ago
parent
commit
57b25313da
2 changed files with 39 additions and 6 deletions
  1. 18 4
      src/Routing/RouteBuilder.php
  2. 21 2
      tests/TestCase/Routing/RouteBuilderTest.php

+ 18 - 4
src/Routing/RouteBuilder.php

@@ -945,8 +945,14 @@ class RouteBuilder
      * Routes connected in the scoped collection will have the correct path segment
      * prepended, and have a matching plugin routing key set.
      *
+     * ### Options
+     *
+     * - `path` The path prefix to use. Defaults to `Inflector::dasherize($name)`.
+     * - `_namePrefix` Set a prefix used for named routes. The prefix is prepended to the
+     *   name of any route created in a scope callback.
+     *
      * @param string $name The plugin name to build routes for
-     * @param array|callable $options Either the options to use, or a callback
+     * @param array|callable $options Either the options to use, or a callback to build routes.
      * @param callable|null $callback The callback to invoke that builds the plugin routes
      *   Only required when $options is defined.
      * @return void
@@ -957,11 +963,14 @@ class RouteBuilder
             $callback = $options;
             $options = [];
         }
-        $params = ['plugin' => $name] + $this->_params;
         if (empty($options['path'])) {
-            $options['path'] = '/' . Inflector::underscore($name);
+            $path = '/' . Inflector::underscore($name);
+        } else {
+            $path = $options['path'];
         }
-        $this->scope($options['path'], $params, $callback);
+        unset($options['path']);
+        $params = ['plugin' => $name] + $options + $this->_params;
+        $this->scope($path, $params, $callback);
     }
 
     /**
@@ -971,6 +980,11 @@ class RouteBuilder
      * added to. This means that both the current path and parameters will be appended
      * to the supplied parameters.
      *
+     * ### Special Keys in $params
+     *
+     * - `_namePrefix` Set a prefix used for named routes. The prefix is prepended to the
+     *   name of any route created in a scope callback.
+     *
      * @param string $path The path to create a scope for.
      * @param array|callable $params Either the parameters to add to routes, or a callback.
      * @param callable|null $callback The callback to invoke that builds the plugin routes.

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

@@ -524,7 +524,7 @@ class RouteBuilderTest extends TestCase
      *
      * @return void
      */
-    public function testNestedPlugin()
+    public function testPlugin()
     {
         $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
         $res = $routes->plugin('Contacts', function ($r) {
@@ -546,7 +546,7 @@ class RouteBuilderTest extends TestCase
      *
      * @return void
      */
-    public function testNestedPluginPathOption()
+    public function testPluginPathOption()
     {
         $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
         $routes->plugin('Contacts', ['path' => '/people'], function ($r) {
@@ -556,6 +556,25 @@ class RouteBuilderTest extends TestCase
     }
 
     /**
+     * Test creating sub-scopes with plugin() + namePrefix option
+     *
+     * @return void
+     */
+    public function testPluginNamePrefix()
+    {
+        $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
+        $routes->plugin('Contacts', ['_namePrefix' => 'contacts.'], function (RouteBuilder $r) {
+            $this->assertEquals('contacts.', $r->namePrefix());
+        });
+
+        $routes = new RouteBuilder($this->collection, '/b', ['key' => 'value']);
+        $routes->namePrefix('default.');
+        $routes->plugin('Blog', ['_namePrefix' => 'blog.'], function (RouteBuilder $r) {
+            $this->assertEquals('default.blog.', $r->namePrefix(), 'Should combine nameprefix');
+        });
+    }
+
+    /**
      * Test connecting resources.
      *
      * @return void