diff --git a/app/Config/routes.php b/app/Config/routes.php index 72ef866d8..e41f4a667 100644 --- a/app/Config/routes.php +++ b/app/Config/routes.php @@ -30,3 +30,8 @@ * ...and connect the rest of 'Pages' controller's urls. */ Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display')); + +/** + * Load the CakePHP default routes. + */ + require CAKE . 'Config' . DS . 'routes.php'; diff --git a/lib/Cake/Config/routes.php b/lib/Cake/Config/routes.php new file mode 100644 index 000000000..da76d63e3 --- /dev/null +++ b/lib/Cake/Config/routes.php @@ -0,0 +1,83 @@ + $value) { + $plugins[$key] = Inflector::underscore($value); + } + $pluginPattern = implode('|', $plugins); + $match = array('plugin' => $pluginPattern); + $shortParams = array('routeClass' => 'PluginShortRoute', 'plugin' => $pluginPattern); + + foreach ($prefixes as $prefix) { + $params = array('prefix' => $prefix, $prefix => true); + $indexParams = $params + array('action' => 'index'); + Router::connect("/{$prefix}/:plugin", $indexParams, $shortParams); + Router::connect("/{$prefix}/:plugin/:controller", $indexParams, $match); + Router::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match); + } + Router::connect('/:plugin', array('action' => 'index'), $shortParams); + Router::connect('/:plugin/:controller', array('action' => 'index'), $match); + Router::connect('/:plugin/:controller/:action/*', array(), $match); + } + + foreach ($prefixes as $prefix) { + $params = array('prefix' => $prefix, $prefix => true); + $indexParams = $params + array('action' => 'index'); + Router::connect("/{$prefix}/:controller", $indexParams); + Router::connect("/{$prefix}/:controller/:action/*", $params); + } + Router::connect('/:controller', array('action' => 'index')); + Router::connect('/:controller/:action/*'); + + $namedConfig = Router::namedConfig(); + if ($namedConfig['rules'] === false) { + Router::connectNamed(true); + } diff --git a/lib/Cake/Routing/Router.php b/lib/Cake/Routing/Router.php index 2106074e3..0747bdcb9 100644 --- a/lib/Cake/Routing/Router.php +++ b/lib/Cake/Routing/Router.php @@ -145,21 +145,6 @@ class Router { */ protected static $_requests = array(); -/** - * Keeps Router state to determine if default routes have already been connected - * - * @var boolean - * @access private - */ - protected static $_defaultsMapped = false; - -/** - * Keeps track of whether the connection of default routes is enabled or disabled. - * - * @var boolean - */ - protected static $_connectDefaults = true; - /** * Initial state is popualated the first time reload() is called which is at the bottom * of this file. This is a cheat as get_class_vars() returns the value of static vars even if they @@ -413,19 +398,6 @@ class Router { return self::$_namedConfig; } -/** - * Tell router to connect or not connect the default routes. - * - * If default routes are disabled all automatic route generation will be disabled - * and you will need to manually configure all the routes you want. - * - * @param boolean $connect Set to true or false depending on whether you want or don't want default routes. - * @return void - */ - public static function defaults($connect = true) { - self::$_connectDefaults = $connect; - } - /** * Creates REST resource routes for the given controller(s) * @@ -479,15 +451,8 @@ class Router { * @return array Parsed elements from URL */ public static function parse($url) { - if (!self::$_defaultsMapped && self::$_connectDefaults) { - self::__connectDefaultRoutes(); - } - $out = array( - 'pass' => array(), - 'named' => array() - ); - $ext = null; + $out = array(); if ($url && strpos($url, '/') !== 0) { $url = '/' . $url; @@ -547,74 +512,6 @@ class Router { return compact('ext', 'url'); } -/** - * Connects the default, built-in routes, including prefix and plugin routes. The following routes are created - * in the order below: - * - * For each of the Routing.prefixes the following routes are created. Routes containing `:plugin` are only - * created when your application has one or more plugins. - * - * - `/:prefix/:plugin` a plugin shortcut route. - * - `/:prefix/:plugin/:action/*` a plugin shortcut route. - * - `/:prefix/:plugin/:controller` - * - `/:prefix/:plugin/:controller/:action/*` - * - `/:prefix/:controller` - * - `/:prefix/:controller/:action/*` - * - * If plugins are found in your application the following routes are created: - * - * - `/:plugin` a plugin shortcut route. - * - `/:plugin/:action/*` a plugin shortcut route. - * - `/:plugin/:controller` - * - `/:plugin/:controller/:action/*` - * - * And lastly the following catch-all routes are connected. - * - * - `/:controller' - * - `/:controller/:action/*' - * - * You can disable the connection of default routes with Router::defaults(). - * - * @return void - * @access private - */ - private static function __connectDefaultRoutes() { - if ($plugins = CakePlugin::loaded()) { - App::uses('PluginShortRoute', 'Routing/Route'); - foreach ($plugins as $key => $value) { - $plugins[$key] = Inflector::underscore($value); - } - $pluginPattern = implode('|', $plugins); - $match = array('plugin' => $pluginPattern); - $shortParams = array('routeClass' => 'PluginShortRoute', 'plugin' => $pluginPattern); - - foreach (self::$_prefixes as $prefix) { - $params = array('prefix' => $prefix, $prefix => true); - $indexParams = $params + array('action' => 'index'); - self::connect("/{$prefix}/:plugin", $indexParams, $shortParams); - self::connect("/{$prefix}/:plugin/:controller", $indexParams, $match); - self::connect("/{$prefix}/:plugin/:controller/:action/*", $params, $match); - } - self::connect('/:plugin', array('action' => 'index'), $shortParams); - self::connect('/:plugin/:controller', array('action' => 'index'), $match); - self::connect('/:plugin/:controller/:action/*', array(), $match); - } - - foreach (self::$_prefixes as $prefix) { - $params = array('prefix' => $prefix, $prefix => true); - $indexParams = $params + array('action' => 'index'); - self::connect("/{$prefix}/:controller", $indexParams); - self::connect("/{$prefix}/:controller/:action/*", $params); - } - self::connect('/:controller', array('action' => 'index')); - self::connect('/:controller/:action/*'); - - if (self::$_namedConfig['rules'] === false) { - self::connectNamed(true); - } - self::$_defaultsMapped = true; - } - /** * Takes parameter and path information back from the Dispatcher, sets these * parameters as the current request parameters that are merged with url arrays diff --git a/lib/Cake/Test/Case/Routing/RouterTest.php b/lib/Cake/Test/Case/Routing/RouterTest.php index 49117693c..2a9236432 100644 --- a/lib/Cake/Test/Case/Routing/RouterTest.php +++ b/lib/Cake/Test/Case/Routing/RouterTest.php @@ -115,7 +115,7 @@ class RouterTest extends CakeTestCase { $_SERVER['REQUEST_METHOD'] = 'GET'; $result = Router::parse('/posts/add'); - $this->assertEqual($result, array('pass' => array(), 'named' => array(), 'plugin' => '', 'controller' => 'posts', 'action' => 'add')); + $this->assertEquals(array(), $result); Router::reload(); $resources = Router::mapResources('Posts', array('id' => '[a-z0-9_]+')); @@ -446,58 +446,6 @@ class RouterTest extends CakeTestCase { ))); $expected = '/tests/index/namedParam[keyed]:is an array/namedParam[0]:test'; $this->assertEqual($expected, $result); - - //@todo Delete from here down, tests are in CakeRoute now. - $result = Router::parse('/tests/action/var[]:val1/var[]:val2'); - $expected = array( - 'controller' => 'tests', - 'action' => 'action', - 'named' => array( - 'var' => array( - 'val1', - 'val2' - ) - ), - 'pass' => array(), - 'plugin' => null - ); - $this->assertEqual($expected, $result); - - $result = Router::parse('/tests/action/theanswer[is]:42/var[]:val2/var[]:val3'); - $expected = array( - 'controller' => 'tests', - 'action' => 'action', - 'named' => array( - 'theanswer' => array( - 'is' => 42 - ), - 'var' => array( - 'val2', - 'val3' - ) - ), - 'pass' => array(), - 'plugin' => null - ); - $this->assertEqual($expected, $result); - - $result = Router::parse('/tests/action/theanswer[is][not]:42/theanswer[]:5/theanswer[is]:6'); - $expected = array( - 'controller' => 'tests', - 'action' => 'action', - 'named' => array( - 'theanswer' => array( - 5, - 'is' => array( - 6, - 'not' => 42 - ) - ), - ), - 'pass' => array(), - 'plugin' => null - ); - $this->assertEqual($expected, $result); } /** @@ -605,10 +553,6 @@ class RouterTest extends CakeTestCase { 'controller' => 'source|wiki|commits|tickets|comments|view', 'action' => 'branches|history|branch|logs|view|start|add|edit|modify' )); - Router::defaults(false); - $result = Router::parse('/foo/bar'); - $expected = array('pass' => array(), 'named' => array()); - $this->assertEqual($expected, $result); } /** @@ -956,6 +900,7 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::parse('/pages/display/home'); $expected = array('plugin' => null, 'pass' => array('home'), 'controller' => 'pages', 'action' => 'display', 'named' => array()); $this->assertEqual($expected, $result); @@ -1192,6 +1137,7 @@ class RouterTest extends CakeTestCase { CakePlugin::loadAll(); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $request = new CakeRequest(); Router::setRequestInfo( $request->addParams(array( @@ -1210,7 +1156,7 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); - Router::parse('/'); + require CAKE . 'Config' . DS . 'routes.php'; $request = new CakeRequest(); Router::setRequestInfo( $request->addParams(array( @@ -1261,6 +1207,7 @@ class RouterTest extends CakeTestCase { */ public function testExtensionParsing() { Router::parseExtensions(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::parse('/posts.rss'); $expected = array('plugin' => null, 'controller' => 'posts', 'action' => 'index', 'url' => array('ext' => 'rss'), 'pass'=> array(), 'named' => array()); @@ -1278,6 +1225,8 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; + Router::parseExtensions('rss', 'xml'); $result = Router::parse('/posts.xml'); @@ -1434,11 +1383,13 @@ class RouterTest extends CakeTestCase { */ public function testNamedArgsUrlParsing() { Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value'); $expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::connectNamed(false); $this->assertEqual(array_keys($result['rules']), array()); $this->assertFalse($result['greedyNamed']); @@ -1447,52 +1398,43 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::connectNamed(true); $named = Router::namedConfig(); $this->assertEqual(array_keys($result['rules']), $named['default']); $this->assertTrue($result['greedyNamed']); + Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; Router::connectNamed(array('param1' => 'not-matching')); $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param:value'); $expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); $this->assertEqual($expected, $result); - //@todo delete this test. - Router::reload(); - Router::connect('/foo/:action/*', array('controller' => 'bar'), array('named' => array('param1' => array('action' => 'index')), 'greedyNamed' => true)); - $result = Router::parse('/foo/index/param1:value1:1/param2:value2:3/param:value'); - $expected = array('pass' => array(), 'named' => array('param1' => 'value1:1', 'param2' => 'value2:3', 'param' => 'value'), 'controller' => 'bar', 'action' => 'index', 'plugin' => null); - $this->assertEqual($expected, $result); - $result = Router::parse('/foo/view/param1:value1:1/param2:value2:3/param:value'); - $expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'bar', 'action' => 'view', 'plugin' => null); + $expected = array('pass' => array('param1:value1:1'), 'named' => array('param2' => 'value2:3', 'param' => 'value'), 'controller' => 'foo', 'action' => 'view', 'plugin' => null); $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; Router::connectNamed(array('param1' => '[\d]', 'param2' => '[a-z]', 'param3' => '[\d]')); $result = Router::parse('/controller/action/param1:1/param2:2/param3:3'); $expected = array('pass' => array('param2:2'), 'named' => array('param1' => '1', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; Router::connectNamed(array('param1' => '[\d]', 'param2' => true, 'param3' => '[\d]')); $result = Router::parse('/controller/action/param1:1/param2:2/param3:3'); $expected = array('pass' => array(), 'named' => array('param1' => '1', 'param2' => '2', 'param3' => '3'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); $this->assertEqual($expected, $result); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; Router::connectNamed(array('param1' => 'value[\d]+:[\d]+'), array('greedy' => false)); $result = Router::parse('/controller/action/param1:value1:1/param2:value2:3/param3:value'); $expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'controller', 'action' => 'action', 'plugin' => null); $this->assertEqual($expected, $result); - - //@todo delete this test. - Router::reload(); - Router::connect('/foo/*', array('controller' => 'bar', 'action' => 'fubar'), array('named' => array('param1' => 'value[\d]:[\d]'))); - Router::connectNamed(array(), array('greedy' => false)); - $result = Router::parse('/foo/param1:value1:1/param2:value2:3/param3:value'); - $expected = array('pass' => array('param2:value2:3', 'param3:value'), 'named' => array('param1' => 'value1:1'), 'controller' => 'bar', 'action' => 'fubar', 'plugin' => null); - $this->assertEqual($expected, $result); } /** @@ -1761,13 +1703,7 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); Router::reload(); - Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); - - $result = Router::parse('/pages/display/home/parameter:value'); - $expected = array('pass' => array('home'), 'named' => array('parameter' => 'value'), 'plugin' => null, 'controller' => 'pages', 'action' => 'display'); - $this->assertEqual($expected, $result); - - Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home')); $result = Router::parse('/'); @@ -1802,6 +1738,7 @@ class RouterTest extends CakeTestCase { */ public function testParsingWithTrailingPeriod() { Router::reload(); + Router::connect('/:controller/:action/*'); $result = Router::parse('/posts/view/something.'); $this->assertEqual($result['pass'][0], 'something.', 'Period was chopped off %s'); @@ -1816,6 +1753,7 @@ class RouterTest extends CakeTestCase { */ public function testParsingWithTrailingPeriodAndParseExtensions() { Router::reload(); + Router::connect('/:controller/:action/*'); Router::parseExtensions('json'); $result = Router::parse('/posts/view/something.'); @@ -1848,20 +1786,13 @@ class RouterTest extends CakeTestCase { $this->assertEqual($expected, $result); $result = Router::parse('/blog/foobar'); - $expected = array( - 'plugin' => null, - 'controller' => 'blog', - 'action' => 'foobar', - 'pass' => array(), - 'named' => array() - ); - $this->assertEqual($expected, $result); + $this->assertEquals(array(), $result); $result = Router::url(array('controller' => 'blog_posts', 'action' => 'foo')); - $this->assertEqual('/blog_posts/foo', $result); + $this->assertEquals('/blog_posts/foo', $result); $result = Router::url(array('controller' => 'blog_posts', 'action' => 'actions')); - $this->assertEqual('/blog/actions', $result); + $this->assertEquals('/blog/actions', $result); } /** @@ -1932,10 +1863,6 @@ class RouterTest extends CakeTestCase { $result = Router::url(array('members' => true, 'controller' => 'users', 'action' => 'add')); $expected = '/base/members/users/add'; $this->assertEqual($expected, $result); - - $result = Router::parse('/posts/index'); - $expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'index'); - $this->assertEqual($expected, $result); } /** @@ -2068,17 +1995,12 @@ class RouterTest extends CakeTestCase { public function testRegexRouteMatching() { Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng')); - $result = Router::parse('/test/test_action'); - $expected = array('pass' => array(), 'named' => array(), 'controller' => 'test', 'action' => 'test_action', 'plugin' => null); - $this->assertEqual($expected, $result); - $result = Router::parse('/eng/test/test_action'); $expected = array('pass' => array(), 'named' => array(), 'locale' => 'eng', 'controller' => 'test', 'action' => 'test_action', 'plugin' => null); $this->assertEqual($expected, $result); $result = Router::parse('/badness/test/test_action'); - $expected = array('pass' => array('test_action'), 'named' => array(), 'controller' => 'badness', 'action' => 'test', 'plugin' => null); - $this->assertEqual($expected, $result); + $this->assertEquals(array(), $result); Router::reload(); Router::connect('/:locale/:controller/:action/*', array(), array('locale' => 'dan|eng')); @@ -2196,7 +2118,6 @@ class RouterTest extends CakeTestCase { * @return void */ public function testDefaultsMethod() { - Router::defaults(false); Router::connect('/test/*', array('controller' => 'pages', 'action' => 'display', 2)); $result = Router::parse('/posts/edit/5'); $this->assertFalse(isset($result['controller'])); @@ -2216,6 +2137,7 @@ class RouterTest extends CakeTestCase { ), true); CakePlugin::loadAll(); Router::reload(); + require CAKE . 'Config' . DS . 'routes.php'; $result = Router::url(array('plugin' => 'plugin_js', 'controller' => 'js_file', 'action' => 'index')); $this->assertEqual($result, '/plugin_js/js_file');