Well thanks so much delayedinsanity for your help, I figured out why it was not working in the first place, I had the log in form pointing to the wrong file... so when I changed it to the correct one it executed perfectly. Once again thanks.
Also I do have another question about my front controller. It works perfectly when I point it to a file I have made, and it also works when a page cannot be found but I cant figure out how to make it have a default page to display for my index.
Example:
If I go to a page that exists like
http://localhost/rome/index.php?page=user&action=login
Then it loads it up perfectly.
And when I go to a page that doesn't exist like
http://localhost/rome/index.php?page=free&action=money
it displays the 404 error page like it should...
So the question is when I want it to point to the main index file
http://localhost/rome/index.php
How do I get it to load up a default home page when someone goes to the index.php? Because all I get from it is my 404 error page.
index.php
PHP Code:
<?php
define("CONTROLLER_DIR", dirname(__FILE__) . "/app/controller");
define("MODEL_DIR", dirname(__FILE__) . "/app/model");
define("PAGE_DIR", dirname(__FILE__) . "/app/view/pages");
define("WEBROOT_DIR", dirname(__FILE__) . "/app/webroot");
require_once dirname(__FILE__) . '/app/controller/front.php';
FrontController::createInstance()->dispatch();
?>
front.php
PHP Code:
<?php
require_once dirname(__FILE__) . "/controller.php";
require_once dirname(__FILE__) . "/action.php";
require_once dirname(__FILE__) . "/template.php";
class FrontController extends Controller {
public static function createInstance() {
if (!defined("PAGE_DIR")) {
exit("Critical error: Cannot proceed without PAGE_DIR.");
}
if (!defined("CONTROLLER_DIR")) {
exit("Critical error: Cannot proceed without CONTROLLER_DIR.");
}
if (!defined("MODEL_DIR")) {
exit("Critical error: Cannot proceed without MODEL_DIR.");
}
if (!defined("WEBROOT_DIR")) {
exit("Critical error: Cannot proceed without WEBROOT_DIR.");
}
$instance = new self();
return $instance;
}
public function dispatch() {
$page1 = !empty($_GET["page"]) ? $_GET["page"] : "home";
$action1 = !empty($_GET["action"]) ? $_GET["action"] : "index";
$page = preg_replace("/[^a-zA-Z0-9_]+/", "", $page1);
$action = preg_replace("/[^a-zA-Z0-9_]+/", "", $action1);
$this->forward($page, $action);
}
}
?>
action.php
PHP Code:
<?php
require_once dirname(__FILE__) . "/controller.php";
abstract class ActionController extends Controller {
protected $name;
protected $viewData = array();
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
public function setVar($key, $value) {
$this->viewData[$key] = $value;
}
public function getVar($key) {
if (array_key_exists($key, $this->viewData)) {
return $this->viewData[$key];
}
}
public function __set($key, $value) {
$this->setVar($key, $value);
}
public function __get($key) {
return $this->getVar($key);
}
public function dispatchAction($action) {
$actionMethod = "do" . ucfirst($action);
if (!method_exists($this, $actionMethod)) {
//exit("Page not found");
$tpl = & new TemplateController();
$tpl->displayTemplate();
include PAGE_DIR . "/error/404.tpl";
exit;
}
$this->$actionMethod();
$this->displayView($action);
}
public function displayView($action) {
if (!is_file(PAGE_DIR . "/" . $this->getName() . "/" . $action . ".tpl")) {
//exit("Page not found");
$tpl = & new TemplateController();
$tpl->displayTemplate();
include PAGE_DIR . "/error/404.tpl";
exit;
}
//Create variables for the template
foreach ($this->viewData as $key => $value) {
$$key = $value;
}
$tpl = & new TemplateController();
$tpl->displayTemplate();
include PAGE_DIR . "/" . $this->getName() . "/" . $action . ".tpl";
exit(0);
}
}
?>
controller.php
PHP Code:
<?php
require_once 'template.php';
abstract class Controller extends TemplateController {
public function getRequest() {
}
public function getResponse() {
}
public function getSession() {
//I tend to prefer $this->getRequest()->getSession(), but whatever!
}
public function forward($page, $action) {
//e.g. HomeActions
$class = ucfirst($page) . "_controller";
//e.g. pages/home/HomeActions.php
//$file = CONTROLLER_DIR . "/" . $page . "/" . $class . ".php";
$file = CONTROLLER_DIR . "/" . $class . ".php";
if (!is_file($file)) {
//exit("Page not found");
$tpl = & new TemplateController();
$tpl->displayTemplate();
include PAGE_DIR . "/error/404.tpl";
exit;
}
require_once $file;
$controller = new $class();
$controller->setName($page);
$controller->dispatchAction($action);
exit(0);
}
}
?>