From 85a9132c9bb515ed83558bc08997572e6271c48a Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 4 Dec 2013 21:46:59 -0500 Subject: [PATCH] Append / to the start/end of the mapResources prefix. This makes the method easier to use and less error prone. Fixes #2431 --- lib/Cake/Routing/Router.php | 6 ++++++ lib/Cake/Test/Case/Routing/RouterTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index c5dbf660e17..f5ae3847d67 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -503,6 +503,12 @@ public static function mapResources($controller, $options = array()) { ), $options); $prefix = $options['prefix']; + if (strpos($prefix, '/') !== 0) { + $prefix = '/' . $prefix; + } + if (substr($prefix, -1) !== '/') { + $prefix .= '/'; + } foreach ((array)$controller as $name) { list($plugin, $name) = pluginSplit($name); diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index 19144f4e6b7..6a7cf05a7e7 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -215,6 +215,20 @@ public function testPluginMapResourcesWithPrefix() { ); $this->assertEquals($expected, $result); $this->assertEquals(array('test_plugin'), $resources); + + $resources = Router::mapResources('Posts', array('prefix' => 'api')); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + $result = Router::parse('/api/posts'); + $expected = array( + 'pass' => array(), + 'named' => array(), + 'plugin' => null, + 'controller' => 'posts', + 'action' => 'index', + '[method]' => 'GET' + ); + $this->assertEquals($expected, $result); } /**