Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - Implementing the Hierarchical Model-Vi...
MISCELLANEOUS CODE

Implementing the Hierarchical Model-View-Controller Design Pattern in PHP 5
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2010-02-17

    Table of Contents:
  • Implementing the Hierarchical Model-View-Controller Design Pattern in PHP 5
  • Setting the stage for the HMVC design pattern
  • Applying the front controller pattern
  • Completing the framework’s front controller: catching exceptions and routing/dispatching requests

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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:

    (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 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.
    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 ...


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