From 25c9707c5fdbb0e65a51c6d4bde56ef002d4bfba Mon Sep 17 00:00:00 2001 From: Tom Kay Date: Mon, 26 Jan 2015 13:31:30 +0000 Subject: [PATCH] implement 'defaultRoute' method. Used to specify a fallback route, defaults to 'defaultAction' method. --- src/Kernel/CubexKernel.php | 14 +++++++++++-- src/Routing/Router.php | 2 +- tests/Cubex/Kernel/CubexKernelTest.php | 27 ++++++++++++++++++++++++++ tests/res/subnamespaced.php | 22 +++++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/Kernel/CubexKernel.php b/src/Kernel/CubexKernel.php index cfbfcd6..28f128e 100644 --- a/src/Kernel/CubexKernel.php +++ b/src/Kernel/CubexKernel.php @@ -83,7 +83,7 @@ public function getRoutes() } /** - * If no route can be processed, run this action + * The default method to run if no route is found * * @return null */ @@ -92,6 +92,16 @@ public function defaultAction() return null; } + /** + * If no route can be processed, use this route + * + * @return null + */ + public function defaultRoute() + { + return 'defaultAction'; + } + /** * Authentication hook, allowing validation before the router is attempted * @@ -250,7 +260,7 @@ public function findRoute(Request $request) } if(!$route) { - $route = Route::create('defaultAction', $this->_processParams); + $route = Route::create($this->defaultRoute(), $this->_processParams); } return $route; } diff --git a/src/Routing/Router.php b/src/Routing/Router.php index f418fff..de0b97a 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -106,7 +106,7 @@ public function matchPattern($url, $pattern) } //We need a pattern to match, null or empty are too vague - if($pattern === null || empty($pattern)) + if(empty($pattern)) { return false; } diff --git a/tests/Cubex/Kernel/CubexKernelTest.php b/tests/Cubex/Kernel/CubexKernelTest.php index badfc52..3fc2182 100644 --- a/tests/Cubex/Kernel/CubexKernelTest.php +++ b/tests/Cubex/Kernel/CubexKernelTest.php @@ -596,6 +596,33 @@ public function testSubClassRouting() $this->assertContains('test application', (string)$result); } + public function testDefaultRoute() + { + $cubex = new \Cubex\Cubex(); + $cubex->prepareCubex(); + $cubex->processConfiguration($cubex->getConfiguration()); + + $kernel = new \namespaced\CubexProject(); + $kernel->setCubex($cubex); + $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); + $request->server->set('REQUEST_URI', '/test/default'); + $result = $kernel->handle( + $request, + \Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST, + false + ); + $this->assertContains('test default action', (string)$result); + + $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals(); + $request->server->set('REQUEST_URI', '/test/default/test-sub-route'); + $result = $kernel->handle( + $request, + \Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST, + false + ); + $this->assertContains('test sub route', (string)$result); + } + public function invalidRoute() { $cubex = new \Cubex\Cubex(); diff --git a/tests/res/subnamespaced.php b/tests/res/subnamespaced.php index 99a393c..1dadc87 100644 --- a/tests/res/subnamespaced.php +++ b/tests/res/subnamespaced.php @@ -3,6 +3,7 @@ use Cubex\Console\ConsoleCommand; use Cubex\Kernel\ApplicationKernel; +use Cubex\Kernel\CubexKernel; use Cubex\View\ViewModel; class SubRoutable @@ -72,6 +73,27 @@ public function getRoutes() } } +class DefaultExtension extends CubexKernel +{ + public function defaultRoute() + { + return TestSubController::class; + } +} + +class TestSubController extends CubexKernel +{ + public function defaultAction() + { + return 'test default action'; + } + + public function testSubRoute() + { + return 'test sub route'; + } +} + class TestView extends ViewModel { public function render()