Browse Source

Remove default routes.

Remove the default routes file that generates routes based on config
data. Routing in CakePHP 3.0 should be much more declarative and less
magic based.  While the scopes result in slightly more routes being
connected, the performance improvements are shown to offset any slowdown
even for large route sets.
mark_story 11 years ago
parent
commit
566e768cae

+ 0 - 99
src/Config/routes.php

@@ -1,99 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link          http://cakephp.org CakePHP(tm) Project
- * @since         2.0.0
- * @license       http://www.opensource.org/licenses/mit-license.php MIT License
- */
-use Cake\Core\Plugin;
-use Cake\Routing\Router;
-use Cake\Utility\Inflector;
-
-/**
- * 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/: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/:controller`
- * - `/:plugin/:controller/:action/*`
- *
- * And lastly the following catch-all routes are connected.
- *
- * - `/:controller'
- * - `/:controller/:action/*'
- *
- * You can disable the connection of default routes by deleting the require inside APP/Config/routes.php.
- */
-
-$prefixes = Router::prefixes();
-$prefixPattern = implode('|', $prefixes);
-$plugins = Plugin::loaded();
-foreach ($plugins as $key => $value) {
-	$plugins[$key] = Inflector::underscore($value);
-}
-$pluginPattern = implode('|', $plugins);
-$indexParams = ['action' => 'index'];
-$pluginShortMatch = [
-	'routeClass' => 'Cake\Routing\Route\PluginShortRoute',
-	'_name' => '_plugin._controller:index',
-	'defaultRoute' => true
-];
-
-if ($prefixPattern && $pluginPattern) {
-	$match = [
-		'prefix' => $prefixPattern,
-		'plugin' => $pluginPattern,
-		'defaultRoute' => true,
-		'routeClass' => 'Cake\Routing\Route\InflectedRoute'
-	];
-	Router::connect('/:prefix/:plugin', $indexParams, $pluginShortMatch + $match);
-	Router::connect('/:prefix/:plugin/:controller', $indexParams, $match);
-	Router::connect('/:prefix/:plugin/:controller/:action/*', [], $match);
-}
-if ($pluginPattern) {
-	$match = [
-		'plugin' => $pluginPattern,
-		'defaultRoute' => true,
-		'routeClass' => 'Cake\Routing\Route\InflectedRoute'
-	];
-	Router::connect('/:plugin', $indexParams, $pluginShortMatch + $match);
-	Router::connect('/:plugin/:controller', $indexParams, $match);
-	Router::connect('/:plugin/:controller/:action/*', [], $match);
-}
-if ($prefixPattern) {
-	$match = [
-		'prefix' => $prefixPattern,
-		'defaultRoute' => true,
-		'routeClass' => 'Cake\Routing\Route\InflectedRoute'
-	];
-	Router::connect('/:prefix/:controller', $indexParams, $match);
-	Router::connect('/:prefix/:controller/:action/*', [], $match);
-}
-Router::connect('/:controller', ['action' => 'index'], [
-	'defaultRoute' => true,
-	'routeClass' => 'Cake\Routing\Route\InflectedRoute'
-]);
-Router::connect('/:controller/:action/*', [], [
-	'defaultRoute' => true,
-	'routeClass' => 'Cake\Routing\Route\InflectedRoute'
-]);
-
-unset($prefixes, $prefixPattern, $plugins, $pluginPattern, $indexParams, $match);

+ 21 - 27
tests/TestCase/Controller/Component/AuthComponentTest.php

@@ -53,13 +53,6 @@ class AuthComponentTest extends TestCase {
 	public $fixtures = ['core.user', 'core.auth_user'];
 
 /**
- * initialized property
- *
- * @var bool
- */
-	public $initialized = false;
-
-/**
  * setUp method
  *
  * @return void
@@ -70,6 +63,10 @@ class AuthComponentTest extends TestCase {
 		Configure::write('Security.salt', 'YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
 		Configure::write('App.namespace', 'TestApp');
 
+		Router::scope('/', function($routes) {
+			$routes->fallbacks();
+		});
+
 		$request = new Request();
 		$response = $this->getMock('Cake\Network\Response', array('stop'));
 
@@ -78,10 +75,6 @@ class AuthComponentTest extends TestCase {
 
 		$this->Auth = new TestAuthComponent($this->Controller->components());
 
-		$this->initialized = true;
-		Router::reload();
-		Router::connect('/:controller/:action/*');
-
 		$Users = TableRegistry::get('AuthUsers');
 		$Users->updateAll(['password' => Security::hash('cake', 'blowfish', false)], []);
 	}
@@ -524,12 +517,14 @@ class AuthComponentTest extends TestCase {
 			'AuthUsers' => array('id' => '1', 'username' => 'nate')
 		));
 
-		$this->Auth->request->addParams(Router::parse('Users/login'));
-		$this->Auth->request->url = 'Users/login';
+		$this->Auth->request->addParams(Router::parse('users/login'));
+		$this->Auth->request->url = 'users/login';
 		$this->Auth->request->env('HTTP_REFERER', false);
 
 		$this->Auth->config('loginRedirect', [
-			'controller' => 'pages', 'action' => 'display', 'welcome'
+			'controller' => 'pages',
+			'action' => 'display',
+			'welcome'
 		]);
 		$event = new Event('Controller.startup', $this->Controller);
 		$this->Auth->startup($event);
@@ -555,7 +550,7 @@ class AuthComponentTest extends TestCase {
 		]);
 		$event = new Event('Controller.startup', $this->Controller);
 		$this->Auth->startup($event);
-		$expected = Router::normalize('/AuthTest/login');
+		$expected = Router::normalize('/auth_test/login');
 		$this->assertEquals($expected, $this->Controller->testUrl);
 
 		$this->Auth->session->delete('Auth');
@@ -642,7 +637,7 @@ class AuthComponentTest extends TestCase {
 
 		// External Direct Login Link
 		$this->Auth->session->delete('Auth');
-		$url = '/AuthTest/login';
+		$url = '/auth_test/login';
 		$this->Auth->request = $this->Controller->request = new Request($url);
 		$this->Auth->request->env('HTTP_REFERER', 'http://webmail.example.com/view/message');
 		$this->Auth->request->addParams(Router::parse($url));
@@ -863,10 +858,9 @@ class AuthComponentTest extends TestCase {
  */
 	public function testAdminRoute() {
 		$event = new Event('Controller.startup', $this->Controller);
-		$pref = Configure::read('Routing.prefixes');
-		Configure::write('Routing.prefixes', array('admin'));
-		Router::reload();
-		require CAKE . 'Config/routes.php';
+		Router::prefix('admin', function($routes) {
+			$routes->fallbacks();
+		});
 
 		$url = '/admin/auth_test/add';
 		$this->Auth->request->addParams(Router::parse($url));
@@ -876,13 +870,13 @@ class AuthComponentTest extends TestCase {
 		Router::setRequestInfo($this->Auth->request);
 
 		$this->Auth->config('loginAction', [
-			'prefix' => 'admin', 'controller' => 'auth_test', 'action' => 'login'
+			'prefix' => 'admin',
+			'controller' => 'auth_test',
+			'action' => 'login'
 		]);
 
 		$this->Auth->startup($event);
 		$this->assertEquals('/admin/auth_test/login', $this->Controller->testUrl);
-
-		Configure::write('Routing.prefixes', $pref);
 	}
 
 /**
@@ -918,9 +912,9 @@ class AuthComponentTest extends TestCase {
  */
 	public function testLoginActionRedirect() {
 		$event = new Event('Controller.startup', $this->Controller);
-		Configure::write('Routing.prefixes', array('admin'));
-		Router::reload();
-		require CAKE . 'Config/routes.php';
+		Router::prefix('admin', function($routes) {
+			$routes->fallbacks();
+		});
 
 		$url = '/admin/auth_test/login';
 		$request = $this->Auth->request;
@@ -1309,6 +1303,6 @@ class AuthComponentTest extends TestCase {
 
 		$this->assertInstanceOf('Cake\Network\Response', $this->Auth->startup($event));
 
-		$this->assertEquals('/Users/login', $this->Controller->testUrl);
+		$this->assertEquals('/users/login', $this->Controller->testUrl);
 	}
 }

+ 16 - 111
tests/TestCase/Routing/RouterTest.php

@@ -1242,7 +1242,7 @@ class RouterTest extends TestCase {
 		$this->assertEquals($expected, $result);
 
 		Router::reload();
-		require CAKE . 'Config/routes.php';
+		$this->_connectDefaultRoutes();
 		$result = Router::parse('/pages/display/home');
 		$expected = array(
 			'plugin' => null,
@@ -1480,7 +1480,7 @@ class RouterTest extends TestCase {
  * @dataProvider parseReverseSymmetryData
  */
 	public function testParseReverseSymmetry($url) {
-		require CAKE . 'Config/routes.php';
+		$this->_connectDefaultRoutes();
 		$this->assertSame($url, Router::reverse(Router::parse($url) + array('url' => [])));
 	}
 
@@ -1523,77 +1523,6 @@ class RouterTest extends TestCase {
 	}
 
 /**
- * Test prefix routing and plugin combinations
- *
- * @return void
- */
-	public function testPrefixRoutingAndPlugins() {
-		Configure::write('Routing.prefixes', array('admin'));
-		$paths = App::path('Plugin');
-		Plugin::load(array('TestPlugin'));
-
-		Router::reload();
-		require CAKE . 'Config/routes.php';
-		$request = new Request();
-		Router::setRequestInfo(
-			$request->addParams(array(
-				'controller' => 'controller',
-				'action' => 'action',
-				'plugin' => null,
-				'prefix' => 'admin'
-			))->addPaths(array(
-				'base' => '/',
-				'here' => '/',
-				'webroot' => '/base/',
-			))
-		);
-
-		$result = Router::url(array(
-			'plugin' => 'test_plugin',
-			'controller' => 'test_plugin',
-			'action' => 'index'
-		));
-		$expected = '/admin/test_plugin';
-		$this->assertEquals($expected, $result);
-
-		Router::reload();
-		require CAKE . 'Config/routes.php';
-		$request = new Request();
-		Router::setRequestInfo(
-			$request->addParams(array(
-				'plugin' => 'test_plugin',
-				'controller' => 'show_tickets',
-				'action' => 'edit',
-				'pass' => array('6'),
-				'prefix' => 'admin',
-			))->addPaths(array(
-				'base' => '/',
-				'here' => '/admin/shows/show_tickets/edit/6',
-				'webroot' => '/',
-			))
-		);
-
-		$result = Router::url(array(
-			'plugin' => 'test_plugin',
-			'controller' => 'show_tickets',
-			'action' => 'edit',
-			6,
-			'prefix' => 'admin'
-		));
-		$expected = '/admin/test_plugin/show_tickets/edit/6';
-		$this->assertEquals($expected, $result);
-
-		$result = Router::url(array(
-			'plugin' => 'test_plugin',
-			'controller' => 'show_tickets',
-			'action' => 'index',
-			'prefix' => 'admin'
-		));
-		$expected = '/admin/test_plugin/show_tickets';
-		$this->assertEquals($expected, $result);
-	}
-
-/**
  * testParseExtensions method
  *
  * @return void
@@ -1613,7 +1542,7 @@ class RouterTest extends TestCase {
 		Router::parseExtensions('rss', false);
 		$this->assertContains('rss', Router::extensions());
 
-		require CAKE . 'Config/routes.php';
+		$this->_connectDefaultRoutes();
 
 		$result = Router::parse('/posts.rss');
 		$this->assertEquals('rss', $result['_ext']);
@@ -1631,7 +1560,7 @@ class RouterTest extends TestCase {
  */
 	public function testExtensionParsing() {
 		Router::parseExtensions('rss', false);
-		require CAKE . 'Config/routes.php';
+		$this->_connectDefaultRoutes();
 
 		$result = Router::parse('/posts.rss');
 		$expected = array(
@@ -1659,7 +1588,7 @@ class RouterTest extends TestCase {
 
 		Router::reload();
 		Router::parseExtensions(['rss', 'xml'], false);
-		require CAKE . 'Config/routes.php';
+		$this->_connectDefaultRoutes();
 
 		$result = Router::parse('/posts.xml');
 		$expected = array(
@@ -1991,7 +1920,6 @@ class RouterTest extends TestCase {
 		$this->assertEquals($expected, $result);
 
 		Router::reload();
-		require CAKE . 'Config/routes.php';
 		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
 
 		$result = Router::parse('/');
@@ -2296,40 +2224,6 @@ class RouterTest extends TestCase {
 	}
 
 /**
- * test that the required default routes are connected.
- *
- * @return void
- */
-	public function testConnectDefaultRoutes() {
-		Plugin::load(array('TestPlugin', 'PluginJs'));
-		Router::reload();
-		require CAKE . 'Config/routes.php';
-
-		$result = Router::url(array('plugin' => 'PluginJs', 'controller' => 'JsFile', 'action' => 'index'));
-		$this->assertEquals('/plugin_js/js_file', $result);
-
-		$result = Router::parse('/plugin_js/js_file');
-		$expected = array(
-			'plugin' => 'PluginJs', 'controller' => 'JsFile', 'action' => 'index',
-			'pass' => []
-		);
-		$this->assertEquals($expected, $result);
-
-		$result = Router::url(array('plugin' => 'test_plugin', 'controller' => 'test_plugin', 'action' => 'index'));
-		$this->assertEquals('/test_plugin', $result, 'Plugin shortcut route generation failed.');
-
-		$result = Router::parse('/test_plugin');
-		$expected = array(
-			'plugin' => 'TestPlugin',
-			'controller' => 'TestPlugin',
-			'action' => 'index',
-			'pass' => []
-		);
-
-		$this->assertEquals($expected, $result, 'Plugin shortcut route broken.');
-	}
-
-/**
  * test using a custom route class for route connection
  *
  * @return void
@@ -2794,4 +2688,15 @@ class RouterTest extends TestCase {
 		});
 	}
 
+/**
+ * Connect some fallback routes for testing router behavior.
+ *
+ * @return void
+ */
+	protected function _connectDefaultRoutes() {
+		Router::scope('/', function($routes) {
+			$routes->fallbacks();
+		});
+	}
+
 }

+ 17 - 5
tests/TestCase/TestSuite/ControllerTestCaseTest.php

@@ -49,10 +49,25 @@ class ControllerTestCaseTest extends TestCase {
 		Plugin::load(array('TestPlugin', 'TestPluginTwo'));
 
 		$this->Case = $this->getMockForAbstractClass('Cake\TestSuite\ControllerTestCase');
+		$this->Case->loadRoutes = false;
+
 		DispatcherFactory::add('Routing');
 		DispatcherFactory::add('ControllerFactory');
-		Router::reload();
-		require CAKE . 'Config/routes.php';
+		Router::scope('/', function($routes) {
+			$routes->fallbacks();
+		});
+		Router::prefix('admin', function($routes) {
+			$routes->plugin('TestPlugin', function ($routes) {
+				$routes->fallbacks();
+			});
+			$routes->fallbacks();
+		});
+		Router::plugin('TestPlugin', function($routes) {
+			$routes->fallbacks();
+		});
+		Router::plugin('TestPluginTwo', function($routes) {
+			$routes->fallbacks();
+		});
 		TableRegistry::clear();
 	}
 
@@ -205,9 +220,6 @@ class ControllerTestCaseTest extends TestCase {
  * @return void
  */
 	public function testActionWithPrefix() {
-		Configure::write('Routing.prefixes', ['admin']);
-		Plugin::load('TestPlugin');
-
 		$result = $this->Case->testAction('/admin/posts/index', ['return' => 'view']);
 		$expected = '<h1>Admin Post Index</h1>';
 		$this->assertContains($expected, $result);

+ 2 - 2
tests/TestCase/Utility/FolderTest.php

@@ -617,7 +617,7 @@ class FolderTest extends TestCase {
 		$this->assertSame(array_diff($expected, $result), array());
 
 		$result = $Folder->find('.*', true);
-		$expected = array('cacert.pem', 'config.php', 'routes.php');
+		$expected = array('cacert.pem', 'config.php');
 		$this->assertSame($expected, $result);
 
 		$result = $Folder->find('.*\.php');
@@ -626,7 +626,7 @@ class FolderTest extends TestCase {
 		$this->assertSame(array_diff($expected, $result), array());
 
 		$result = $Folder->find('.*\.php', true);
-		$expected = array('config.php', 'routes.php');
+		$expected = array('config.php');
 		$this->assertSame($expected, $result);
 
 		$result = $Folder->find('.*ig\.php');

+ 5 - 10
tests/test_app/TestApp/Config/routes.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * Routes file
- *
- * Routes for test app
- *
  * CakePHP : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -20,11 +16,10 @@ namespace TestApp\Config;
 
 use Cake\Routing\Router;
 
-// Configure::write('Routing.prefixes', array());
 
 Router::parseExtensions('json');
-Router::connect('/some_alias', array('controller' => 'tests_apps', 'action' => 'some_method'));
-
-Router::connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
-
-require CAKE . 'Config/routes.php';
+Router::scope('/', function($routes) {
+	$routes->connect('/', ['controller' => 'pages', 'action' => 'display', 'home']);
+	$routes->connect('/some_alias', array('controller' => 'tests_apps', 'action' => 'some_method'));
+	$routes->fallbacks();
+});