-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1626e50
commit ca62257
Showing
10 changed files
with
142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
class Router { | ||
/** | ||
* Path/controller associative array | ||
* @var array | ||
*/ | ||
public static $routes = array(); | ||
/** | ||
* Path/parameter associative array | ||
* @var array | ||
*/ | ||
public static $route_parameters=array(); | ||
/** | ||
* Route request and return results of appropriate controller call. | ||
* @return str | ||
*/ | ||
public function route($session_started=false) { | ||
$url = explode('?',$_SERVER['REQUEST_URI']); | ||
$path = mb_strtolower($url[0]); | ||
while (substr($path, -1) == '/') { | ||
$path = mb_substr($path,0,(mb_strlen($path)-1)); | ||
} | ||
$path_components = explode('/', $path); | ||
|
||
$slug = $path_components[1]; | ||
$slug = ($slug=='')?'index':$slug; | ||
if (isset(self::$routes[$slug])) { | ||
if (isset(self::$route_parameters[$slug])) { | ||
foreach (self::$route_parameters[$slug] as $index=>$parameter) { | ||
$_GET[$parameter] = $path_components[$index+2]; | ||
} | ||
} | ||
$controller = new self::$routes[$slug]($session_started); | ||
return $controller->go(); | ||
} else { | ||
return "404 route not found: ".$slug; | ||
} | ||
} | ||
/** | ||
* Add route. | ||
* @param $slug | ||
* @param $controller_name | ||
*/ | ||
public function addRoute($slug, $controller_name, $parameters=null) { | ||
self::$routes[$slug] = $controller_name; | ||
self::$route_parameters[$slug] = $parameters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
{include file="_header.tpl"} | ||
|
||
<a href="{$site_root_path}{$logo_link}">{$app_title}</a>: {$test} | {if isset($logged_in_user)}Logged in as {$logged_in_user}{else}Not logged in{/if} | ||
<a href="{$site_root_path}{$logo_link}">{$app_title}</a>: {$test} | {if isset($logged_in_user)}Logged in as {$logged_in_user}{else}Not logged in{/if} | ||
{if isset($username) and isset($network)} | ||
<br><br><br> | ||
Username:{$username}<br> | ||
Network:{$network} | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
require_once dirname(__FILE__).'/init.tests.php'; | ||
require_once ISOSCELES_PATH.'extlibs/simpletest/autorun.php'; | ||
require_once ISOSCELES_PATH.'libs/config.inc.php'; | ||
|
||
class TestOfRouter extends IsoscelesBasicUnitTestCase { | ||
|
||
public function setUp(){ | ||
parent::setUp(); | ||
} | ||
|
||
public function tearDown(){ | ||
parent::tearDown(); | ||
} | ||
|
||
public function testConstructor() { | ||
$router = new Router(); | ||
$this->assertTrue(isset($router), 'constructor test'); | ||
} | ||
|
||
public function testAddRoute() { | ||
$router = new Router(); | ||
$router->addRoute('test', 'TestController'); | ||
$this->assertEqual(array('test'=>'TestController'), Router::$routes); | ||
$this->assertEqual(1, sizeof(Router::$routes)); | ||
|
||
$router->addRoute('test2', 'Test2Controller'); | ||
$this->assertEqual(array('test'=>'TestController', 'test2'=>'Test2Controller'), Router::$routes); | ||
$this->assertEqual(2, sizeof(Router::$routes)); | ||
} | ||
|
||
public function testRouteNoParameters() { | ||
$router = new Router(); | ||
$router->addRoute('test', 'TestController'); | ||
$router->addRoute('index', 'TestController'); | ||
|
||
$_SERVER['REQUEST_URI'] = "/"; | ||
$results = $router->route(true); | ||
$this->assertPattern('/My Isosceles Application/', $results); | ||
|
||
|
||
$_SERVER['REQUEST_URI'] = "/test/user/ginatrapani"; | ||
$results = $router->route(true); | ||
$this->assertPattern('/My Isosceles Application/', $results); | ||
|
||
$_SERVER['REQUEST_URI'] = "/nonexistent/user/ginatrapani"; | ||
$results = $router->route(true); | ||
$this->assertPattern('/404 route not found: nonexistent/', $results); | ||
} | ||
|
||
public function testRouteWithParameters() { | ||
$router = new Router(); | ||
$router->addRoute('user', 'TestController', array('username', 'network')); | ||
|
||
$_SERVER['REQUEST_URI'] = "/user/twitter/username"; | ||
$results = $router->route(true); | ||
$this->assertPattern('/My Isosceles Application/', $results); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
<?php | ||
require_once dirname(dirname(__FILE__)).'/libs/model/class.Loader.php'; | ||
Loader::register(); | ||
$controller = new TestController(); | ||
echo $controller->control(); | ||
|
||
$router = new Router(); | ||
$router->addRoute('test', 'TestController', array('network', 'username')); | ||
$router->addRoute('index', 'TestController'); | ||
echo $router->route(); |