Home arrow PHP arrow Page 4 - Building a Simple MVC-based Framework in PHP 5

Bootstrapping the dispatcher module - PHP

In this first part of a series, I develop the first module of a model-view-controller (MVC) driven framework, which happens to be a front controller. By combining this component and a basic .htaccess file, it’s possible to route all HTTP requests to the (still-undefined) dispatcher class.

  1. Building a Simple MVC-based Framework in PHP 5
  2. Routing all HTTP requests to the front controller
  3. Defining the framework’s front controller
  4. Bootstrapping the dispatcher module
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
June 07, 2010

print this article



In reality, making the front controller bootstrap the dispatcher module mentioned in the previous section is reduced to writing one single line of code. It’s really that easy, believe me.

To demonstrate this, below I included the finished version of the controller, which is now also capable of intercepting exceptions through a “try-catch” block. Look at the completed definition of the “index.php” file:


// framework’s front controller

// specify parameters for autoloading classes

spl_autoload_register(NULL, FALSE);


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




                                $file = $class . '.php';

                                if (!file_exists($file))


                                                eval('class ' . $class . '{}');

                                                throw new Exception('File ' . $file . ' not found.');




                                if (!class_exists($class, FALSE))


                                                eval('class ' . $class . '{}');

                                                throw new ClassNotFoundException('Class ' . $class . ' not found.');




// handle request and dispatch it to the appropriate controller




catch (ClassNotFoundException $e){

                echo $e->getMessage();



catch (Exception $e){

                echo $e->getMessage();


}// End front controller

Unquestionably, at this point the front controller looks much more useful, since it can now autoload classes, catch exceptions, and most important, bootstrap the corresponding dispatcher module through this single line of code:


Obviously, this means that the dispatcher is a class that has that same name, and that it also implements a static method called “dispatch().” However, if you’re eager and want to see how this class looks, then I suggest you take a deep breath and relax, since the development of the dispatcher will be discussed in depth in the next tutorial.

Meanwhile, feel free to tweak all of the example code shown in this article, which might be pretty useful for enhancing the capabilities of the front controller file coded before. 

Final thoughts

In this introductory chapter of the series, I developed the first module of this MVC-driven framework, which happens to be a front controller. As you saw for yourself, by combining this component and a basic .htaccess file it’s possible to route all of the HTTP requests to the still-undefined dispatcher class.

But the wait won’t be long at all, as this class will be built in the next installment of this series. Now you don’t have any excuses to miss it!

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: