Load routes as late as possible.

As a concequence - routes will also work by default in the cli
This commit is contained in:
AD7six 2012-07-03 19:32:41 +02:00
parent 57f81da983
commit 5140baf83d
3 changed files with 35 additions and 15 deletions

View file

@ -209,12 +209,6 @@ class Dispatcher implements CakeEventListener {
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 @@ class Dispatcher implements CakeEventListener {
return false;
}
/**
* Loads route configuration
*
* @return void
*/
protected function _loadRoutes() {
include APP . 'Config' . DS . 'routes.php';
}
}

View file

@ -48,6 +48,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 @@ class Router {
* @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 @@ class Router {
* @return array Parsed elements from URL
*/
public static function parse($url) {
if (!self::$initialized) {
self::_loadRoutes();
}
$ext = null;
$out = array();
@ -748,6 +761,10 @@ class Router {
* @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 @@ class Router {
* @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 @@ class Router {
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 file

@ -2339,9 +2339,13 @@ class RouterTest extends CakeTestCase {
/**
* 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',