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