"Router" class to determine current Controller (Page)
Posted: Sun Aug 08, 2010 3:33 pm
Hello, this is my first post on devnetwork. I'm looking forward to participating.
I am looking for critique (obviously) of a design I have come up with for a PHP-based website. Specifically, this is a content management system.
The idea is that the client registers legal Controller class names with an instance of the Router class (seen in index.php below). The Router instance retrieves the currently specified Controller class name via GET or POST and then determines if it is a legal name; if it is legal, then the controller is instantiated, if it is not legal, then the page is redirected and a default Controller is display (such as a MainMenu class for example).
I am utilizing the Model-View-Controller design for the controller classes, but am specifically interested in feedback about my Router class. I don't have a peer-group of programmers to fall on and am looking forward to some constructive criticism! Thanks!
Here are two UML diagrams for my setup:
Router: http://www.miketmoore.com/shared/websit ... ter_01.jpg
Controllers: http://www.miketmoore.com/shared/websit ... ers_01.jpg
index.php:
`ARouter` class:
`Router` class:
I am looking for critique (obviously) of a design I have come up with for a PHP-based website. Specifically, this is a content management system.
The idea is that the client registers legal Controller class names with an instance of the Router class (seen in index.php below). The Router instance retrieves the currently specified Controller class name via GET or POST and then determines if it is a legal name; if it is legal, then the controller is instantiated, if it is not legal, then the page is redirected and a default Controller is display (such as a MainMenu class for example).
I am utilizing the Model-View-Controller design for the controller classes, but am specifically interested in feedback about my Router class. I don't have a peer-group of programmers to fall on and am looking forward to some constructive criticism! Thanks!
Here are two UML diagrams for my setup:
Router: http://www.miketmoore.com/shared/websit ... ter_01.jpg
Controllers: http://www.miketmoore.com/shared/websit ... ers_01.jpg
index.php:
Code: Select all
//Instantiate Router instance
$router = new Router();
//Register legal Controller class names and their include URLs with Router instance
$router->register('GeneralError', 'include/dir/GeneralError.php');
$router->register('MainMenu', 'include/dir/MainMenu.php');
$router->register('GalleryManager', 'include/dir/GalleryManager.php');
//Initiate the Router instance
$router->route();
`ARouter` class:
Code: Select all
abstract class ARouter
{
private $legalClassNames = array();
abstract public function route();
abstract protected function defaultRoute();
public function register($className, $includeUrl)
{
$this->legalClassNames[] = array('className' => $className, 'includeUrl' => $includeUrl);
}
final protected function isLegal($className)
{
foreach($this->legalClassNames as $array)
{
if(in_array($className, $array)) {
return true;
}
}
return false;
}
final protected function includeByClassName($className)
{
$includeUrl = $this->getIncludeUrlByClassName($className);
include $includeUrl;
}
private function getIncludeUrlByClassName($className)
{
foreach($this->legalClassNames as $array)
{
if(in_array($className, $array)) {
return $array['includeUrl'];
}
}
return NULL;
}
}
Code: Select all
class Router extends ARouter
{
protected function defaultRoute()
{
header('Location: index.php?page_name=MainMenu');
}
public function route()
{
if(($_SERVER['REQUEST_METHOD'] == 'POST') || ($_SERVER['REQUEST_METHOD'] == 'GET'))
{
//Request type is GET or POST
$this->getPageName();
}
else
{
//Request type is not GET or POST
$this->defaultRoute();
}
}
protected function getPageName()
{
if(!isset($_REQUEST['page_name'])) {
$this->defaultRoute();
} else if(empty($_REQUEST['page_name'])) {
$this->defaultRoute();
} else if(!$this->isLegal($_REQUEST['page_name'])) {
$this->defaultRoute();
} else {
//Page name is a legal class name
$this->initPage($_REQUEST['page_name']);
}
}
private function initPage($className)
{
$this->includeByClassName($className);
$this->page = new $className();
$this->page->init();
}
}