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

Fixes #1956
This commit is contained in:
Mark Story mark@mark-story.com 2011-09-01 18:44:15 +01:00
parent b5bee102df
commit 41b22c5521
2 changed files with 88 additions and 8 deletions

View file

@ -396,7 +396,9 @@ class Router {
} }
/** /**
* 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: * ### Options:
* *
@ -409,21 +411,32 @@ class Router {
* @return array Array of mapped resources * @return array Array of mapped resources
*/ */
public static function mapResources($controller, $options = array()) { public static function mapResources($controller, $options = array()) {
$hasPrefix = isset($options['prefix']);
$options = array_merge(array( $options = array_merge(array(
'prefix' => '/', 'prefix' => '/',
'id' => self::ID . '|' . self::UUID 'id' => self::ID . '|' . self::UUID
), $options); ), $options);
$prefix = $options['prefix']; $prefix = $options['prefix'];
foreach ((array)$controller as $ctlName) { foreach ((array)$controller as $name) {
$urlName = Inflector::underscore($ctlName); list($plugin, $name) = pluginSplit($name);
$urlName = Inflector::underscore($name);
$plugin = Inflector::underscore($plugin);
if ($plugin && !$hasPrefix) {
$prefix = '/' . $plugin . '/';
}
foreach (self::$_resourceMap as $params) { foreach (self::$_resourceMap as $params) {
extract($params); $url = $prefix . $urlName . (($params['id']) ? '/:id' : '');
$url = $prefix . $urlName . (($id) ? '/:id' : '');
Router::connect($url, 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')) array('id' => $options['id'], 'pass' => array('id'))
); );
} }

View file

@ -78,11 +78,11 @@ class RouterTest extends CakeTestCase {
} }
/** /**
* testResourceRoutes method * testMapResources method
* *
* @return void * @return void
*/ */
public function testResourceRoutes() { public function testMapResources() {
$resources = Router::mapResources('Posts'); $resources = Router::mapResources('Posts');
$_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_METHOD'] = 'GET';
@ -126,6 +126,73 @@ class RouterTest extends CakeTestCase {
$this->assertEqual($result, array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT')); $this->assertEqual($result, array('pass' => array('name'), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => 'name', '[method]' => 'PUT'));
} }
/**
* 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 * testMultipleResourceRoute method
* *