Browse Source

Allow any inflector method to be used when creating resource routes.

Mark Story 11 years ago
parent
commit
5624d3e18b
2 changed files with 24 additions and 1 deletions
  1. 3 1
      src/Routing/RouteBuilder.php
  2. 21 0
      tests/TestCase/Routing/RouteBuilderTest.php

+ 3 - 1
src/Routing/RouteBuilder.php

@@ -232,6 +232,7 @@ class RouteBuilder
      *
      * - 'id' - The regular expression fragment to use when matching IDs. By default, matches
      *    integer values and UUIDs.
+     * - 'inflect' - Choose the inflection method used on the resource name. Defaults to 'underscore'.
      * - 'only' - Only connect the specific list of actions.
      * - 'actions' - Override the method names used for connecting actions.
      * - 'map' - Additional resource routes that should be connected. If you define 'only' and 'map',
@@ -251,6 +252,7 @@ class RouteBuilder
         }
         $options += [
             'connectOptions' => [],
+            'inflect' => 'underscore',
             'id' => static::ID . '|' . static::UUID,
             'only' => [],
             'actions' => [],
@@ -267,7 +269,7 @@ class RouteBuilder
         }
 
         $connectOptions = $options['connectOptions'];
-        $urlName = Inflector::underscore($name);
+        $urlName = Inflector::{$options['inflect']}($name);
         $resourceMap = array_merge(static::$_resourceMap, $options['map']);
 
         $only = (array)$options['only'];

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

@@ -345,6 +345,27 @@ class RouteBuilderTest extends TestCase
     }
 
     /**
+     * Test connecting resources with the inflection option
+     *
+     * @return void
+     */
+    public function testResourcesInflection()
+    {
+        $routes = new RouteBuilder($this->collection, '/api', ['prefix' => 'api']);
+        $routes->resources('BlogPosts', ['_ext' => 'json', 'inflect' => 'dasherize']);
+
+        $all = $this->collection->routes();
+        $this->assertCount(5, $all);
+
+        $this->assertEquals('/api/blog-posts', $all[0]->template);
+        $this->assertEquals(
+            ['controller', 'action', '_method', 'prefix', 'plugin'],
+            array_keys($all[0]->defaults)
+        );
+        $this->assertEquals('BlogPosts', $all[0]->defaults['controller']);
+    }
+
+    /**
      * Test connecting resources with additional mappings
      *
      * @return void