Skip to content

Commit

Permalink
Add a simple oneliner way to add fallback routes to a scope.
Browse files Browse the repository at this point in the history
I found I was repeating these same 2 lines in many places. Having
a simple macro for them will make working with routes a bit less
repetitive.
  • Loading branch information
markstory committed Jul 3, 2014
1 parent 99d8d42 commit 9d9453a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Routing/ScopedRouteCollection.php
Expand Up @@ -626,4 +626,16 @@ public function merge(ScopedRouteCollection $collection) {
$this->_named += $collection->named();
}

/**
* Connect the `/:controller` and `/:controller/:action/*` fallback routes.
*
* This is a shortcut method for connecting fallback routes in a given scope.
*
* @return void
*/
public function fallbacks() {
$this->connect('/:controller');
$this->connect('/:controller/:action/*');
}

}
14 changes: 14 additions & 0 deletions tests/TestCase/Routing/ScopedRouteCollectionTest.php
Expand Up @@ -379,4 +379,18 @@ public function testResourcesNested() {
});
}

/**
* Test connecting fallback routes.
*
* @return void
*/
public function testFallbacks() {
$routes = new ScopedRouteCollection('/api', ['prefix' => 'api']);
$routes->fallbacks();

$all = $routes->routes();
$this->assertEquals('/api/:controller', $all[0]->template);
$this->assertEquals('/api/:controller/:action/*', $all[1]->template);
}

}

0 comments on commit 9d9453a

Please sign in to comment.