So, I've been working on this framework for a bit and my index.php has been added to and removed from constantly... It seems to be a mess now. It works quite well and it does what its suppose to but I Just feel it could be cleaner and maybe even processed better/faster. Anyway, here's my index.php:
<?php
/**
* index.php - The controller for the framework.
*
* @version 0.0.7
* @author Dylan Greer <thekarnedge (AT) gmail (DOT) com>
* @copyright Copyright (C) 2009 - ctrlGames, Inc. All rights reserved.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
* @package bbgFramework
*/
require_once('config.php');
/**
* @param (string) $class Class name we're trying to load
*/
function __autoload($class){
if(preg_match('|^\w+$|',$class))
$file = str_replace('_','/',$class).'.php';
include _PATHBASE.'includes/'.$file;
}
try{
$URI = explode("/",(string) $_SERVER['REQUEST_URI']);
unset($URI[0]);
if(strlen(end($URI)) < 1){ array_pop($URI); }
$module = (isset($URI[1])) ? array_shift($URI) : 'home';
$class = (isset($URI[0])) ? array_shift($URI) : $module;
$classPath = _PATHBASE.'modules/'.$module.'/';
if(!file_exists($classPath.$class.'.php')){
array_unshift($URI,$class);
$class = $module;
}
if(!file_exists($classPath.$class.'.php')){ throw new Exception("Could not find: $classPath$class.php"); }
require_once($classPath.$class.'.php');
if(!class_exists($class)){ throw new Exception("A valid module class was not found."); }
$instance = new $class();
if(!Module::isValid($instance)){ throw new Exception("Requested module is not a valid framework module!"); }
$instance->moduleName = $module;
$instance->URI = $URI;
if($instance->authenticate()){
$result = $instance->__default();
$display = Display::factory($instance->display,$instance);
$display->display();
}else{
header("Location: /account/login");
}
}catch(Exception $error){
echo '<code><strong>Fatal Error: </strong>'.$error->getMessage().'</code>';
}
?>- The autoload obviously loads up any classes it needs on the fly from the includes dir, if there is an underscore it will pull it from a deeper dir. So Auth_User would be "_PATHBASE/includes/auth/user.php"
- The next part seperates the URI into a usable array. As it's set up it loads the modules and classes like "/modulename/classname/other/vars/used", if the classname doesn't exist it will use the modulename as the class (eg;
http://www.domain.com/ loads "/modules/home/home.php" with no variables OR
http://www.domain.com/account/lost/t390d9jdkldj0 loads "/modules/account/lost.php" with 't390d9jdkldj0' as the 1st variable)
- It will load the classname into an instance if it exists then checks if it isValid which just makes sure that the Object class is extending from Module.php and Authenticate.php classes in the /includes directory
- It will load the moduleName and URI variables into the instance so they can eventually be used/called in the display part (smarty)
- The module will check the session if the user is logged in or not and then check to make sure the module requires authentication or none, Auth_No always returns true so it doesn't matter if you're logged in or not, Auth_User requires the user actually be logged in and will return true if so... otherwise it will redirect the user to login
- If anyone paid attention to some of my previous posts about my framework, I use to use the URI to load a specific event in the class but I realized, I will probably NEVER use it lol so I just load __default() event
- Then we use the factory method to load the display which utilizes the Smarty system to load templates which automatically load templates for each module by their name... also, there is a wrapper tpl for the entire site so <head></head> info isn't repeated in each module plus you can put in navigation menus... that way module tpl's focus on their actual purpose.
- Then if all else fails (literally) it will collect any thrown Exceptions (which include modules or includes) and display it... although, any exception should only be displayed if there are coding errors (or hacks) not the user's fault, so technically its useless for production.
Anyway, that was a bit long and exhaustive in explanation... the point is, i was hoping to get some feedback to make my framework controller better.