diff --git a/src/Routing/ScopedRouteCollection.php b/src/Routing/ScopedRouteCollection.php index 942dc0804ed..5a7641f0c69 100644 --- a/src/Routing/ScopedRouteCollection.php +++ b/src/Routing/ScopedRouteCollection.php @@ -219,6 +219,10 @@ public function get($name) { * @return array Array of mapped resources */ public function resource($name, $options = [], $callback = null) { + if (is_callable($options) && $callback === null) { + $callback = $options; + $options = []; + } $options += array( 'connectOptions' => [], 'id' => static::ID . '|' . static::UUID @@ -227,8 +231,8 @@ public function resource($name, $options = [], $callback = null) { unset($options['connectOptions']); $urlName = Inflector::underscore($name); - $ext = null; + $ext = null; if (!empty($options['_ext'])) { $ext = $options['_ext']; } @@ -251,7 +255,7 @@ public function resource($name, $options = [], $callback = null) { if (is_callable($callback)) { $idName = Inflector::singularize($urlName) . '_id'; - $path = '/' . $urlName . '/:' . $idName; + $path = $this->_path . '/' . $urlName . '/:' . $idName; Router::scope($path, $this->params(), $callback); } } diff --git a/tests/TestCase/Routing/ScopedRouteCollectionTest.php b/tests/TestCase/Routing/ScopedRouteCollectionTest.php index cd3e190e333..65e25f4bcd2 100644 --- a/tests/TestCase/Routing/ScopedRouteCollectionTest.php +++ b/tests/TestCase/Routing/ScopedRouteCollectionTest.php @@ -125,6 +125,8 @@ public function testConnectBasic() { */ public function testConnectExtensions() { $routes = new ScopedRouteCollection('/l', [], ['json']); + $this->assertEquals(['json'], $routes->extensions()); + $routes->connect('/:controller'); $route = $routes->routes()[0]; @@ -288,4 +290,62 @@ public function testMatch() { $this->assertFalse($result, 'No matches'); } +/** + * Test matching plugin routes. + * + * @return void + */ + public function testMatchPlugin() { + $context = [ + '_base' => '/', + '_scheme' => 'http', + '_host' => 'example.org', + ]; + $routes = new ScopedRouteCollection('/contacts', ['plugin' => 'Contacts']); + $routes->connect('/', ['controller' => 'Contacts']); + + $result = $routes->match( + ['plugin' => 'Contacts', 'controller' => 'Contacts', 'action' => 'index'], + $context + ); + $this->assertFalse($result); + + $result = $routes->match(['controller' => 'Contacts', 'action' => 'index'], $context); + $this->assertFalse($result); + } + +/** + * Test connecting resources. + * + * @return void + */ + public function testResource() { + $routes = new ScopedRouteCollection('/api', ['prefix' => 'api']); + $routes->resource('Articles', ['_ext' => 'json']); + + $all = $routes->routes(); + $this->assertCount(6, $all); + + $this->assertEquals('/api/articles', $all[0]->template); + $this->assertEquals('json', $all[0]->defaults['_ext']); + $this->assertEquals('Articles', $all[0]->defaults['controller']); + } + +/** + * Test nesting resources + * + * @return void + */ + public function testResourceNested() { + $routes = new ScopedRouteCollection('/api', ['prefix' => 'api']); + $routes->resource('Articles', function($routes) { + $this->assertEquals('/api/articles/', $routes->path()); + $this->assertEquals(['prefix' => 'api'], $routes->params()); + + $routes->resource('Comments'); + $route = $routes->routes()[0]; + $this->assertEquals('/api/articles/:article_id/comments', $route->template); + }); + } + }