Browse files

Fixing Router::mapResources() so it works with plugins.

Fixes #1956
  • Loading branch information...
1 parent b5bee10 commit 41b22c55217b56e4ff7267af03e94745dbacfe3a @markstory markstory committed Sep 1, 2011
Showing with 88 additions and 8 deletions.
  1. +19 −6 lib/Cake/Routing/Router.php
  2. +69 −2 lib/Cake/Test/Case/Routing/RouterTest.php
View
25 lib/Cake/Routing/Router.php
@@ -396,7 +396,9 @@ public static function namedConfig() {
}
/**
- * Creates REST resource routes for the given controller(s)
+ * Creates REST resource routes for the given controller(s). When creating resource routes
+ * for a plugin, by default the prefix will be changed to the lower_underscore version of the plugin
+ * name. By providing a prefix you can override this behavior.
*
* ### Options:
*
@@ -409,21 +411,32 @@ public static function namedConfig() {
* @return array Array of mapped resources
*/
public static function mapResources($controller, $options = array()) {
+ $hasPrefix = isset($options['prefix']);
$options = array_merge(array(
'prefix' => '/',
'id' => self::ID . '|' . self::UUID
), $options);
+
$prefix = $options['prefix'];
- foreach ((array)$controller as $ctlName) {
- $urlName = Inflector::underscore($ctlName);
+ foreach ((array)$controller as $name) {
+ list($plugin, $name) = pluginSplit($name);
+ $urlName = Inflector::underscore($name);
+ $plugin = Inflector::underscore($plugin);
+ if ($plugin && !$hasPrefix) {
+ $prefix = '/' . $plugin . '/';
+ }
foreach (self::$_resourceMap as $params) {
- extract($params);
- $url = $prefix . $urlName . (($id) ? '/:id' : '');
+ $url = $prefix . $urlName . (($params['id']) ? '/:id' : '');
Router::connect($url,
- array('controller' => $urlName, 'action' => $action, '[method]' => $params['method']),
+ array(
+ 'plugin' => $plugin,
+ 'controller' => $urlName,
+ 'action' => $params['action'],
+ '[method]' => $params['method']
+ ),
array('id' => $options['id'], 'pass' => array('id'))
);
}
View
71 lib/Cake/Test/Case/Routing/RouterTest.php
@@ -78,11 +78,11 @@ public function testRouteDefaultParams() {
}
/**
- * testResourceRoutes method
+ * testMapResources method
*
* @return void
*/
- public function testResourceRoutes() {
+ public function testMapResources() {
$resources = Router::mapResources('Posts');
$_SERVER['REQUEST_METHOD'] = 'GET';
@@ -127,6 +127,73 @@ public function testResourceRoutes() {
}
/**
+ * testMapResources with plugin controllers.
+ *
+ * @return void
+ */
+ public function testPluginMapResources() {
+ App::build(array(
+ 'plugins' => array(
+ CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
+ )
+ ));
+ $resources = Router::mapResources('TestPlugin.TestPlugin');
+
+ $_SERVER['REQUEST_METHOD'] = 'GET';
+ $result = Router::parse('/test_plugin/test_plugin');
+ $expected = array(
+ 'pass' => array(),
+ 'named' => array(),
+ 'plugin' => 'test_plugin',
+ 'controller' => 'test_plugin',
+ 'action' => 'index',
+ '[method]' => 'GET'
+ );
+ $this->assertEqual($result, $expected);
+ $this->assertEqual($resources, array('test_plugin'));
+
+ $_SERVER['REQUEST_METHOD'] = 'GET';
+ $result = Router::parse('/test_plugin/test_plugin/13');
+ $expected = array(
+ 'pass' => array('13'),
+ 'named' => array(),
+ 'plugin' => 'test_plugin',
+ 'controller' => 'test_plugin',
+ 'action' => 'view',
+ 'id' => '13',
+ '[method]' => 'GET'
+ );
+ $this->assertEqual($result, $expected);
+ }
+
+/**
+ * Test mapResources with a plugin and prefix.
+ *
+ * @return void
+ */
+ public function testPluginMapResourcesWithPrefix() {
+ App::build(array(
+ 'plugins' => array(
+ CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS
+ )
+ ));
+ $resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => '/api/'));
+
+ $_SERVER['REQUEST_METHOD'] = 'GET';
+ $result = Router::parse('/api/test_plugin');
+ $expected = array(
+ 'pass' => array(),
+ 'named' => array(),
+ 'plugin' => 'test_plugin',
+ 'controller' => 'test_plugin',
+ 'action' => 'index',
+ '[method]' => 'GET'
+ );
+ $this->assertEqual($result, $expected);
+ $this->assertEqual($resources, array('test_plugin'));
+ }
+
+/**
* testMultipleResourceRoute method
*
* @return void

0 comments on commit 41b22c5

Please sign in to comment.