As I said in the section that you just read, the framework’s front controller will be responsible for performing a few simple tasks, such as autoloading classes on request, invoking the router/dispatcher module, and handling exceptions that may be thrown by other components. Based on these requisites, the initial definition of the “index.php” file (read the front controller), will look as follows: // specify parameters for autoloading classes spl_autoload_register(NULL, FALSE); spl_autoload_extensions('.php'); spl_autoload_register(array('Autoloader', 'load')); // 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)) { eval('class ' . $class . '{}'); 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.'); } } } As seen above, the partial definition of the front controller is very easy to follow. First, it uses the group of PHP functions included with the SPL library to register in the stack the class (and its corresponding method) responsible for autoloading requested classes. Once the registering process is complete, the controller includes the definition of the autoloader class, which not surprisingly has been called “Autoloader” as well. Basically, all that the “load()” method of this class does is include a specified class only when required, and throw the corresponding exceptions if the process fails for some reason. At this stage, the front controller of this simple MVC-based framework is definitely starting to take shape, since it’s been provided with autoloading and basic exception handling capabilities. So, what’s the next step? Well, despite the aforementioned abilities, in its current state the front controller isn’t very useful. It's not capable of evaluating a given HTTP request and dispatching it to the appropriate action controller. While it’s fair to note that performing these specific tasks will be a responsibility of the dispatcher component, which logically hasn’t been coded yet, it’s necessary to add, within the front controller, the code that bootstraps this dispatcher module. That’s exactly what I’m going to do in the section to come. So click on the link shown below and read the following segment.
blog comments powered by Disqus |
|
|
|
|
|
|
|