Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 2 - Dispatching Requests to MVC Triads wit...
MISCELLANEOUS CODE

Dispatching Requests to MVC Triads with the Hierarchical MVC Design Pattern
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2010-02-24

    Table of Contents:
  • Dispatching Requests to MVC Triads with the Hierarchical MVC Design Pattern
  • Review: the partial source files of the HMVC-based framework
  • Routing and dispatching HTTP requests with a basic dispatcher class
  • Adding a basic HMVC layer to the framework with a request-handling class

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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
    More By Alejandros Gervasio

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap