| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
Dispatching Requests to MVC Triads with the Hierarchical MVC Design Pattern - Review: the partial source files of the HMVC-based framework(Page 2 of 4 ) As I said in the introduction, for the time being the HMVC-driven framework that I’m developing here is only made up of two source files. The first one is responsible for directing all user requests to “index.php,” and the second one is the front controller, tasked with autoloading classes, handling exceptions and bootstrapping the router/dispatcher class as well. Before I continue adding more functionality to the framework, I’d like to reintroduce the definitions of the aforementioned files, so you can study them in more detail. That said, here’s the “.htaccess” file that redirects all of the HTTP requests to “index.php.” Take a look at it: # Turn on URL rewriting engine RewriteEngine On # Disable rewriting for existing files or directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # redirect all other requests to index.php rewriteRule ^.*$ index.php [PT,L] Understanding how the above file work should be relatively easy for you, even if you’re not completely familiar with coding “.htaccess” files for the Apache web server. Please look at the definition of the framework’s front controller, which is as follows: (index.php)
<?php
// application's front controller
// 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)) { 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 controller try{ Dispatcher::dispatch(); } // catch exceptions catch (ClassNotFoundException $e){ echo $e->getMessage(); exit(); } catch (Exception $e){ echo $e->getMessage(); exit(); }// End front controller From the previous code fragment, it’s clear to see that the front controller performs a few simple (yet crucial) tasks, such as intercepting both custom and default exceptions, autoloading core and user-defined classes on demand, and also bootstrapping the pertinent dispatcher module. As you’ll see, this last component will dispatch a given HTTP request to a specified MVC triad, but before you grasp its underlying logic, it’s necessary to show you its corresponding definition. This definition will be covered in depth in the following section. Therefore, to get there, simply click on the link that appears below and read the next few lines. More Miscellaneous Code Articles |
| |
| |