Skip to content

Commit

Permalink
Make Router::mapResources() use prefixes like everything else.
Browse files Browse the repository at this point in the history
Make prefix carry the same meaning for mapResources() as it does
everywhere else in Router.

Fixes #968
  • Loading branch information
markstory committed Jul 4, 2012
1 parent c68f8b2 commit 9b4f7e9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
48 changes: 27 additions & 21 deletions lib/Cake/Routing/Router.php
Expand Up @@ -96,12 +96,12 @@ class Router {
* @var array
*/
protected static $_resourceMap = array(
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'edit', 'method' => 'POST', 'id' => true)
array('action' => 'index', 'method' => 'GET', 'id' => false),
array('action' => 'view', 'method' => 'GET', 'id' => true),
array('action' => 'add', 'method' => 'POST', 'id' => false),
array('action' => 'edit', 'method' => 'PUT', 'id' => true),
array('action' => 'delete', 'method' => 'DELETE', 'id' => true),
array('action' => 'edit', 'method' => 'POST', 'id' => true)
);

/**
Expand Down Expand Up @@ -341,7 +341,8 @@ public static function redirect($route, $url, $options = array()) {
*
* - 'id' - The regular expression fragment to use when matching IDs. By default, matches
* integer values and UUIDs.
* - 'prefix' - URL prefix to use for the generated routes. Defaults to '/'.
* - 'prefix' - Routing prefix to use for the generated routes. Defaults to ''.
* Using this option will create prefixed routes, similar to using Routing.prefixes.
*
* @param string|array $controller A controller name or array of controller names (i.e. "Posts" or "ListItems")
* @param array $options Options to use when generating REST routes
Expand All @@ -350,34 +351,39 @@ public static function redirect($route, $url, $options = array()) {
public static function mapResources($controller, $options = array()) {
$hasPrefix = isset($options['prefix']);

This comment has been minimized.

Copy link
@majna

majna Jul 23, 2012

Contributor

$hasPrefix not used

This comment has been minimized.

Copy link
@markstory

markstory Jul 23, 2012

Author Member

Thanks, fixed in [5500f94]

$options = array_merge(array(
'prefix' => '/',
'id' => static::ID . '|' . static::UUID
), $options);

$prefix = $options['prefix'];

foreach ((array)$controller as $name) {
list($plugin, $name) = pluginSplit($name);
$urlName = Inflector::underscore($name);

if ($plugin) {
$plugin = Inflector::underscore($plugin);
}
if ($plugin && !$hasPrefix) {
$prefix = '/' . $plugin . '/';
$prefix = '';
if (!empty($options['prefix'])) {
$prefix = $options['prefix'];
}

foreach (static::$_resourceMap as $params) {
$url = $prefix . $urlName . (($params['id']) ? '/:id' : '');

Router::connect($url,
array(
'plugin' => $plugin,
'controller' => $urlName,
'action' => $params['action'],
'[method]' => $params['method']
),
array('id' => $options['id'], 'pass' => array('id'))
$id = $params['id'] ? ':id' : '';
$url = '/' . implode('/', array_filter(array($prefix, $plugin, $urlName, $id)));
$params = array(
'plugin' => $plugin,
'controller' => $urlName,
'action' => $params['action'],
'[method]' => $params['method'],
);
if ($prefix) {
$params['prefix'] = $prefix;
}
$options = array(
'id' => $options['id'],
'pass' => array('id')
);
Router::connect($url, $params, $options);
}
static::$_resourceMapped[] = $urlName;
}
Expand Down
44 changes: 39 additions & 5 deletions lib/Cake/Test/TestCase/Routing/RouterTest.php
Expand Up @@ -98,14 +98,24 @@ public function testMapResources() {

$_SERVER['REQUEST_METHOD'] = 'PUT';
$result = Router::parse('/posts/13');
$this->assertEquals($result, array('pass' => array('13'), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT'));
$expected = array('pass' => array('13'), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '13', '[method]' => 'PUT');
$this->assertEquals($expected, $result);

$result = Router::parse('/posts/475acc39-a328-44d3-95fb-015000000000');
$this->assertEquals($result, array('pass' => array('475acc39-a328-44d3-95fb-015000000000'), 'plugin' => '', 'controller' => 'posts', 'action' => 'edit', 'id' => '475acc39-a328-44d3-95fb-015000000000', '[method]' => 'PUT'));
$expected = array(
'pass' => array('475acc39-a328-44d3-95fb-015000000000'),
'plugin' => '',
'controller' => 'posts',
'action' => 'edit',
'id' => '475acc39-a328-44d3-95fb-015000000000',
'[method]' => 'PUT'
);
$this->assertEquals($expected, $result);

$_SERVER['REQUEST_METHOD'] = 'DELETE';
$result = Router::parse('/posts/13');
$this->assertEquals($result, array('pass' => array('13'), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE'));
$expected = array('pass' => array('13'), 'plugin' => '', 'controller' => 'posts', 'action' => 'delete', 'id' => '13', '[method]' => 'DELETE');
$this->assertEquals($expected, $result);

$_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/posts/add');
Expand Down Expand Up @@ -162,6 +172,29 @@ public function testPluginMapResources() {
$this->assertEquals($expected, $result);
}

/**
* Test mapResources with a prefix.
*
* @return void
*/
public function testMapResourcesWithPrefix() {
$resources = Router::mapResources('Posts', array('prefix' => 'api'));
$this->assertEquals(array('posts'), $resources);

$_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/api/posts');

$expected = array(
'plugin' => null,
'controller' => 'posts',
'action' => 'index',
'pass' => array(),
'prefix' => 'api',
'[method]' => 'GET',
);
$this->assertEquals($expected, $result);
}

/**
* Test mapResources with a plugin and prefix.
*
Expand All @@ -173,14 +206,15 @@ public function testPluginMapResourcesWithPrefix() {
CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS
)
));
$resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => '/api/'));
$resources = Router::mapResources('TestPlugin.TestPlugin', array('prefix' => 'api'));

$_SERVER['REQUEST_METHOD'] = 'GET';
$result = Router::parse('/api/test_plugin');
$result = Router::parse('/api/test_plugin/test_plugin');
$expected = array(
'pass' => array(),
'plugin' => 'test_plugin',
'controller' => 'test_plugin',
'prefix' => 'api',
'action' => 'index',
'[method]' => 'GET'
);
Expand Down

0 comments on commit 9b4f7e9

Please sign in to comment.