HTML and CSS Reference
In-Depth Information
// Tries to initialize the requested view, or else throws a 404 error
try {
$controller = new $class_name($options);
} catch (Exception $e) {
$options[1] = $e->getMessage();
$controller = new Error($options);
}
//-----------------------------------------------------------------------------
// Function declarations
//-----------------------------------------------------------------------------
Using the utility functions, the URI is broken apart and stored in
$uri_array
. It is then passed to
get_controller_classname()
, which stores the controller's class name in
$class_name
. The remaining URI
components are stored in
$options
for later use.
Next,
$class_name
is checked to ensure it isn't empty; if it is, the default class name “Home” is supplied.
Finally, using a
try...catch
block, a new controller object of the requested type is instantiated, passing the
$options
as an argument to the constructor. If anything goes wrong, a new
Error
object is created to display an
error message.
■
Note
You will be building the
Error
class later in this chapter.
Outputting the View
With the controller loaded, there's nothing left to do but output the markup. In
index.php
, add the following
bold code:
//-----------------------------------------------------------------------------
// 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';
}
// Tries to initialize the requested view, or else throws a 404 error
try {
$controller = new $class_name($options);
} catch (Exception $e) {
$options[1] = $e->getMessage();
$controller = new Error($options);
}
Search WWH ::
Custom Search