Browse Source

Append / to the start/end of the mapResources prefix.

This makes the method easier to use and less error prone.

Fixes #2431
mark_story 12 years ago
parent
commit
85a9132c9b
2 changed files with 20 additions and 0 deletions
  1. 6 0
      lib/Cake/Routing/Router.php
  2. 14 0
      lib/Cake/Test/Case/Routing/RouterTest.php

+ 6 - 0
lib/Cake/Routing/Router.php

@@ -503,6 +503,12 @@ class Router {
 		), $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);

+ 14 - 0
lib/Cake/Test/Case/Routing/RouterTest.php

@@ -215,6 +215,20 @@ class RouterTest extends CakeTestCase {
 		);
 		$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);
 	}
 
 /**