Skip to content

Commit

Permalink
implement 'defaultRoute' method. Used to specify a fallback route, de…
Browse files Browse the repository at this point in the history
…faults to 'defaultAction' method.
  • Loading branch information
TomK committed Jan 26, 2015
1 parent 58b107c commit 25c9707
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/Kernel/CubexKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Cubex/Kernel/CubexKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions tests/res/subnamespaced.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Cubex\Console\ConsoleCommand;
use Cubex\Kernel\ApplicationKernel;
use Cubex\Kernel\CubexKernel;
use Cubex\View\ViewModel;

class SubRoutable
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 25c9707

Please sign in to comment.