
Originally Posted by
cyberlot
PHP Code:
if($_REQUEST['page']) {
$method = 'User'.$_REQUEST['page'].'controller';
if(class_exists($method)) {
$pageController = New $method;
}
}
Just wanted to add.. This assumes you already loaded all your librarys from the beginning, so theres no need to do live "taint" and includes based off the REQUEST making things more secure..
You wouldnt want to include all of your libraries on each request. This will cause alot of unnessesary overhead.
A front controller at a minimum needs to have a default page to show if no $_REQUEST['page'] param is given. It also needs to be able to tell the difference between a valid $_REQUEST['page'] and non-valid one, creating the PageController on a valid and sending a 404 or creating the 404PageController on a non valid request.
Heres the example Ive used before:
PHP Code:
class FrontController {
var $defaultPageController;
var $requestPageParam;
var $pageControllers = array();
function FrontController($defaultPageController, $requestPageParam = 'page') {
$this->defaultPageController = $defaultPageController;
$this->requestPageParam = $requestPageParam;
}
function &getPageController() {
$pageControllerName = $this->getPageControllerName();
return $this->getPageControllerInstance($pageControllerName);
}
function addPage($name, $className) {
$this->pageControllers[$name] = $className;
}
function getPageControllerName() {
if (!array_key_exists($this->requestPageParam, $_GET)) {
return $this->defaultPageController;
}
return $_GET[$this->requestPageParam];
}
function &getPageControllerInstance($pageControllerName) {
if (!array_key_exists($pageControllerName, $this->pageControllers)) {
header("HTTP/1.0 404 Not Found");
die();
// Do the above or the below.
require_once './controller/Error404PageController.php';
return new Error404PageController();
}
require_once './controller/' . $this->pageControllers[$pageControllerName] . '.php';
return new $this->pageControllers[$pageControllerName]();
}
}
$fc = new FrontController('index');
$fc->addPage('index', 'IndexController');
$fc->addPage('admin', 'AdminController');
$pc =& $fc->getPageController();
// Below could be moved into the FrontController
$pc->execute();
// Below could be moved into the PageController
$view =& $pc->getView();
$view->render();
Bookmarks