HTML and CSS Reference
In-Depth Information
// Defines all of the valid places a class file could be stored
$possible_locations = array(
SYS_PATH . '/models/class.' . $fname . '.inc.php',
SYS_PATH . '/controllers/class.' . $fname . '.inc.php',
SYS_PATH . '/core/class.' . $fname . '.inc.php',
);
// Loops through the location array and checks for a file to load
foreach ($possible_locations as $loc) {
if (file_exists($loc)) {
require_once $loc;
return TRUE;
}
}
// Fails because a valid class wasn't found
throw new Exception("Class $class_name wasn't found.");
}
Finishing the Router
With the utility functions created and an autoloader in place to load the requested controller, the last step for
the router script is to actually handle the URI components that request the controller and ultimately send the
proper view to the user.
Loading the Controller
In index.php , add the following code between the initialization block and the function declarations:
// Registers class_loader() as the autoload function
spl_autoload_register('class_autoloader');
//-----------------------------------------------------------------------------
// Loads and processes view data
//-----------------------------------------------------------------------------
// Parses the URI
$uri_array = parse_uri();
$class_name = get_controller_classname($uri_array);
$options = $uri_array;
// Sets a default view if nothing is passed in the URI (i.e. on the home page)
if (empty($class_name)) {
$class_name = 'Home';
}
 
Search WWH ::




Custom Search