Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - 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 - Adding a basic HMVC layer to the framework with a request-handling class


    (Page 4 of 4 )

    In the previous section, you learned the definition of the class that comprises the dispatcher module of this HMVC-based framework. Since this class makes use of another one called “Request” to dispatch requests to the correct set of MVC triads, it is necessary to show its full source code, you can understand its internal workings. 

    Given that, below I listed the file that includes the “Request” class, which not surprisingly has the same name. Here it is:

    (Request.php)

     

    <?php

     

    class Request

    {

    private static $_controllerInstance = array();

     

    // private constructor

    private function __construct(){}

     

    // instantiate proper controller with the requested method

    public static function load($controller, $method, $arg = NULL)

    {

    // check if the requested controller has been already instantiated

    if (!isset(self::$_controllerInstance[$controller]))

    {

    // if not, try to instantiate the controller

    if (!class_exists($controller))

    {

    throw new Exception('Call to invalid controller class.');

    }

    self::$_controllerInstance[$controller] = new $controller;

    // check if controller method can be called

    if (!is_callable(array(self::$_controllerInstance[$controller], $method)))

    {

    throw new Exception('Call to invalid controller method.');

    }

    }

    // call method in requested controller and return output when applicable

    return self::$_controllerInstance[$controller]->$method($arg);

    }

    }//End Request class

    Understanding the inner functioning of the previous “Request” class should be easy for you. It simply uses its “load()” method for instantiating the requested controller, in conjunction with its corresponding method and the incoming argument. If any of these processes fail for some reason, a couple of default exceptions will be thrown for further interception.

    Also, it’s important to notice here that the class will attempt to determine whether or not the requested controller has been previously instantiated, to prevent a possible duplication of objects. Other than that, this is all that you need to know about the logic implemented by this static class.

    Now that you've learned how the framework will route and dispatch user requests respectively, you surely realize that the previous “Request” opens the doors to easily developing PHP applications that will be composed of a typical MVC layer and of the so-called MVC triads. Quite possibly, at this time you don’t see very clearly how this can be translated into a real-world example, but once I get the framework finished, all of the pieces will fit neatly, trust me.

    Final thoughts

    In this second installment of the series, I demonstrated how to implement a simple HMVC layer within the sample framework being developed here. This feature was incorporated via a basic request-handling class, which you’re free to improve and extend at will.

    In the next chapter things will become even more interesting, as I’m going to add to the framework a couple of core classes that will give it the ability to work with MySQL and cache database result sets.

    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 5 - Follow our Sitemap