Implementing the Hierarchical Model-View-Controller Design Pattern in PHP 5 - Completing the framework’s front controller: catching exceptions and routing/dispatching requests
(Page 4 of 4 )
As I said before, to finish building the front controller of this sample framework, it’s necessary to give it the capability to route and dispatch single HTTP requests to specific MVC triads, as well as to catch exceptions triggered at runtime. The component that will perform the routing/dispatching process will be a basic class (not defined yet), which will be bootstrapped by the corresponding “index.php” file as shown below:
// define custom ClassNotFoundException exception class
class ClassNotFoundException extends Exception{}
// define Autoloader class
class Autoloader
{
// attempt to autoload a specified class
public static function load($class)
{
if (class_exists($class, FALSE))
{
return;
}
$file = $class . '.php';
if (!file_exists($file))
{
throw new Exception('File ' . $file . ' not found.');
}
require_once($file);
unset($file);
if (!class_exists($class, FALSE))
{
eval('class ' . $class . '{}');
throw new ClassNotFoundException('Class ' . $class . ' not found.');
}
}
}
// handle request and dispatch it to the appropriate MVC triad
try{
Dispatcher::dispatch();
}
// catch exceptions
catch (ClassNotFoundException $e){
echo $e->getMessage();
exit();
}
catch (Exception $e){
echo $e->getMessage();
exit();
}// End front controller
With the inclusion of a single “try-catch” block, the front controller now has the ability to intercept exceptions. On the other hand, the controller also bootstraps the mentioned router/dispatcher class via a static call to its “dispatch()” method, which as you’ll see in upcoming tutorials, will be responsible for routing requests to individual MVC triads, thus implementing the HMVC design pattern.
Meanwhile, feel free to tweak all of the code samples shown in this first part of this series, and introduce your own improvements to the components of this example framework.
Final thoughts
That’s all for now. In this first chapter of the series I started building a simple framework in PHP 5, which I’m going to use for demonstrating how to implement the Hierarchical Model-View-Controller design pattern. As you saw before, the front controller of the framework statically calls a dispatcher class for routing and dispatching HTTP requests to the appropriate MVC triads, which haven’t been defined yet.
However, the construction of this router/dispatcher class will be covered in depth in the upcoming tutorial of the series. So, here’s my little piece of advice: don’t miss the next part!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.