Skip to content

Commit

Permalink
Add Router::defaultRouteClass()
Browse files Browse the repository at this point in the history
Apply patch from 'Tigran Gabrielyan'.
Allows you to set the default route class used for all future
routes.

Fixes #2435
  • Loading branch information
markstory committed Jan 13, 2012
1 parent b76f8f8 commit b14072a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/Cake/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,23 @@ class Router {
*/
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
*/

This comment has been minimized.

Copy link
@ADmad

ADmad Jan 13, 2012

Member

How about making this function a getter too so that if you pass it null param it returns the current default route class name. That could be useful for someone.

This comment has been minimized.

Copy link
@tigrang

tigrang Jan 13, 2012

Contributor

+1 for that, simple to do. I can add a pull request and if you guys can decide if you want it. I guess it also stops for setting $_routeClass to null. Also with this option now I think we also need to check if the class is a sublcass of CakeRoute and throw an exception as currently its only done if $options['routeClass'] is set.

This comment has been minimized.

Copy link
@tigrang

tigrang Jan 13, 2012

Contributor

Fix with getter option here: tigrang@2656e16

This comment has been minimized.

Copy link
@ADmad

ADmad Jan 14, 2012

Member

Can you please make a pull request against the 2.1 branch so that your commit can be easily merged in.

This comment has been minimized.

Copy link
@tigrang

tigrang Jan 14, 2012

Contributor

I thought I did. Sorry, will do now.

public static function defaultRouteClass($routeClass) {
self::$_routeClass = $routeClass;
}

/**
* Sets the Routing prefixes.
*
Expand Down Expand Up @@ -259,7 +276,7 @@ public static function connect($route, $defaults = array(), $options = array())
if (empty($options['action'])) {
$defaults += array('action' => 'index');
}
$routeClass = 'CakeRoute';
$routeClass = self::$_routeClass;
if (isset($options['routeClass'])) {
$routeClass = $options['routeClass'];
if (!is_subclass_of($routeClass, 'CakeRoute')) {
Expand Down
13 changes: 13 additions & 0 deletions lib/Cake/Test/Case/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2506,4 +2506,17 @@ public function testRouteRedirection() {
$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]);
}

}

0 comments on commit b14072a

Please sign in to comment.