Add Router::defaultRouteClass()

Apply patch from 'Tigran Gabrielyan'.
Allows you to set the default route class used for all future
routes.

Fixes #2435
This commit is contained in:
mark_story 2012-01-12 22:19:55 -05:00
parent b76f8f8832
commit b14072aa6f
2 changed files with 31 additions and 1 deletions

View file

@ -152,6 +152,23 @@ class Router {
*/ */
protected static $_initialState = array(); protected static $_initialState = array();
/**
* Default route class to use
*
* @var string
*/
protected static $_routeClass = 'CakeRoute';
/**
* Set the default rotue class to use
*
* @param sting $routeClass to set as default
* @return void
*/
public static function defaultRouteClass($routeClass) {
self::$_routeClass = $routeClass;
}
/** /**
* Sets the Routing prefixes. * Sets the Routing prefixes.
* *
@ -259,7 +276,7 @@ class Router {
if (empty($options['action'])) { if (empty($options['action'])) {
$defaults += array('action' => 'index'); $defaults += array('action' => 'index');
} }
$routeClass = 'CakeRoute'; $routeClass = self::$_routeClass;
if (isset($options['routeClass'])) { if (isset($options['routeClass'])) {
$routeClass = $options['routeClass']; $routeClass = $options['routeClass'];
if (!is_subclass_of($routeClass, 'CakeRoute')) { if (!is_subclass_of($routeClass, 'CakeRoute')) {

View file

@ -2506,4 +2506,17 @@ class RouterTest extends CakeTestCase {
$this->assertEquals(Router::$routes[0]->response->header(), array()); $this->assertEquals(Router::$routes[0]->response->header(), array());
} }
/**
* Test setting the default route class
*
* @return void
*/
public function testDefaultRouteClass() {
$this->getMock('CakeRoute', array(), array('/test'), 'TestDefaultRouteClass');
Router::defaultRouteClass('TestDefaultRouteClass');
$result = Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
$this->assertInstanceOf('TestDefaultRouteClass', $result[0]);
}
} }