I have been working through an excellent OOP front controller tutorial by Chris Corbyn, found here, but have two questions:
(1) Currently, all my "content" is outputted by the ActionController.php5 displayView() function, which I "wrap" in xhtml header/footer file(s) that I am including in my index.php5 (mainly because I don't know where best to include them). Therefore, my question is, where should I be including my xhtml header/footer file(s)? My only goals are to (a) keep it simple (i.e. I am not looking to build/implement a templating system), and (b) keep my presentation logic separate from my business logic.
(2) How can I improve my exception throwing and catching in the FrontController.php5? Again, I want to keep it simple, but still want to make sure I am handling my exceptions appropriately.
I am new to OOP/php, and so I appreciate any feedback you have. Thank you all in advance!
Sincerely,
Brendan
Here are all my files:
Index.php5
Code: Select all
<?php
//Setting full error reporting
ini_set ("display_errors", "1");
error_reporting(E_ALL);
//Setting constant containing root directory for this application
define("PAGE_DIR", "includes/php");
//Initializing session
session_start();
//Including general configuration and function files
require_once PAGE_DIR . "/Configurations.php5";
require_once PAGE_DIR . "/Functions.php5";
//Including header template file(s)
require_once PAGE_DIR . "/template/pre.header.inc.php5";
require_once PAGE_DIR . "/template/header.inc.php5";
require_once PAGE_DIR . "/template/post.header.inc.php5";
////FRONT CONTROLLER////////////////////////////////////////////////////////////////////////////
//Requiring the front controller class
require_once PAGE_DIR . "/FrontController.php5";
FrontController::createInstance(PAGE_DIR)->dispatch(TRUE);
////////////////////////////////////////////////////////////////////////////////////////////////
//Including footer template file(s)
require_once PAGE_DIR . "/template/footer.inc.php5";
?>
FrontController.php5
Code: Select all
<?php
/* Reference(s)
"A lightweight and flexible front controller for PHP 5" by Chris Corbyn
http://www.w3style.co.uk/a-lightweight- ... -for-php-5
*/
require_once PAGE_DIR . "/ActionController.php5";
class FrontController {
private static $pageDir;
public static function createInstance($pageDir){
self::$pageDir = $pageDir;
$instance = new self();
return $instance;
}
public function dispatch($throwExceptions = false) {
//The next two lines are ternary operators, a sort of short-hand for if-else
$module= isset($_GET["module"]) ? filter_input(INPUT_GET, 'module', FILTER_SANITIZE_STRING) : "home";
$action = isset($_GET["action"]) ? filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING) : "frontpage";
//example GuestbookActions
$class = ucfirst($module) . "Actions";
//e.g. guestbook/GuestbookActions.php5
$file = self::$pageDir . "/" . $module . "/" . $class . ".php5";
if (!is_file($file)) {
throw new FrontControllerException("Page not found!");
}
//Requiring the Actions file
require_once $file;
//Creating a new object
$controller = new $class();
//Passing page directory to the ActionController class
$controller->setPageDir(self::$pageDir);
try {
//Using the setName variable from the ActionController class in ActionController.php5
$controller->setName($module);
//Checks if the method exists, then runs the displayView action
$controller->dispatchAction($action);
}
catch(Exception $e){
//An exception has occurred
if($throwExceptions){
echo $e->errorMessage();
}
}
}
}
class FrontControllerException extends Exception {
public function errorMessage(){
//Error message
$errorMsg = $this->getMessage() . ' Error on line ' . $this->getLine() . ' in ' . $this->getFile();
return $errorMsg;
}
}
?>
ActionController.php5
Code: Select all
<?php
abstract class ActionController {
protected $name;
protected $viewData = array();
private static $pageDir;
public static function setPageDir($pageDir){
self::$pageDir = $pageDir;
}
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
//This function places a value in the $viewData array at the postion indicated by the key
public function setVar($key, $value) {
$this->viewData[$key] = $value;
}
//This returns a value from the array located at the key value
public function getVar($key) {
if (array_key_exists($key, $this->viewData)) {
return $this->viewData[$key];
}
}
//method_exists checks if the class method exists in the given object, then runs the displayView function if exists
public function dispatchAction($action) {
$actionMethod = "do" . ucfirst($action);
if (!method_exists($this, $actionMethod)) {
throw new FrontControllerException("Page not found!");
}
$this->$actionMethod();
$this->displayView($action);
}
public function displayView($action) {
if (!is_file(self::$pageDir . "/" . $this->getName() . "/" . $action . "View.php5")) {
throw new FrontControllerException("Page not found!");
}
//Create variables for the template
foreach ($this->viewData as $key => $value) {
$$key = $value;
}
include PAGE_DIR . "/" . $this->getName() . "/" . $action . "View.php5";
}
public function __set($key, $value) {
$this->setVar($key, $value);
}
public function __get($key) {
return $this->getVar($key);
}
}
?>
Thanks again!
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: