Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Load routes as late as possible.

As a concequence - routes will also work by default in the cli
  • Loading branch information...
commit 5140baf83d65e0ea137d43ced4380c63794da424 1 parent 57f81da
@AD7six AD7six authored
View
15 lib/Cake/Routing/Dispatcher.php
@@ -209,12 +209,6 @@ protected function _invoke(Controller $controller, CakeRequest $request, CakeRes
public function parseParams($event) {
$request = $event->data['request'];
Router::setRequestInfo($request);
- if (count(Router::$routes) == 0) {
- $namedExpressions = Router::getNamedExpressions();
- extract($namedExpressions);
- $this->_loadRoutes();
- }
-
$params = Router::parse($request->url);
$request->addParams($params);
@@ -269,13 +263,4 @@ protected function _loadController($request) {
return false;
}
-/**
- * Loads route configuration
- *
- * @return void
- */
- protected function _loadRoutes() {
- include APP . 'Config' . DS . 'routes.php';
- }
-
}
View
31 lib/Cake/Routing/Router.php
@@ -49,6 +49,13 @@ class Router {
public static $routes = array();
/**
+ * Have routes been loaded
+ *
+ * @var boolean
+ */
+ public static $initialized = false;
+
+/**
* List of action prefixes used in connected routes.
* Includes admin prefix
*
@@ -284,6 +291,8 @@ public static function resourceMap($resourceMap = null) {
* @throws RouterException
*/
public static function connect($route, $defaults = array(), $options = array()) {
+ self::$initialized = true;
+
foreach (self::$_prefixes as $prefix) {
if (isset($defaults[$prefix])) {
if ($defaults[$prefix]) {
@@ -520,6 +529,10 @@ public static function prefixes() {
* @return array Parsed elements from URL
*/
public static function parse($url) {
+ if (!self::$initialized) {
+ self::_loadRoutes();
+ }
+
$ext = null;
$out = array();
@@ -748,6 +761,10 @@ public static function promote($which = null) {
* @return string Full translated URL with base path.
*/
public static function url($url = null, $full = false) {
+ if (!self::$initialized) {
+ self::_loadRoutes();
+ }
+
$params = array('plugin' => null, 'controller' => null, 'action' => 'index');
if (is_bool($full)) {
@@ -1117,6 +1134,10 @@ public static function parseExtensions() {
* @return array Array of extensions Router is configured to parse.
*/
public static function extensions() {
+ if (!self::$initialized) {
+ self::_loadRoutes();
+ }
+
return self::$_validExtensions;
}
@@ -1138,6 +1159,16 @@ public static function setExtensions($extensions, $merge = true) {
return self::$_validExtensions = array_merge(self::$_validExtensions, $extensions);
}
+/**
+ * Loads route configuration
+ *
+ * @return void
+ */
+ protected static function _loadRoutes() {
+ self::$initialized = true;
+ include APP . 'Config' . DS . 'routes.php';
+ }
+
}
//Save the initial state
View
4 lib/Cake/Test/Case/Routing/RouterTest.php
@@ -2339,9 +2339,13 @@ public function testCustomRouteException() {
/**
* test reversing parameter arrays back into strings.
*
+ * Mark the router as initialized so it doesn't auto-load routes
+ *
* @return void
*/
public function testRouterReverse() {
+ Router::$initialized = true;
+
$params = array(
'controller' => 'posts',
'action' => 'view',
Please sign in to comment.
Something went wrong with that request. Please try again.