Home arrow PHP arrow Page 2 - Developing the Router/Dispatcher Class for a Simple MVC-based Framework in PHP 5

Review: start building the MVC-based framework - PHP

In this second part of the series, I add another crucial component to this example MVC-driven framework. It's a basic router/dispatcher class. While the incorporation of this brand new module turns the framework into a more functional piece of code, there are other components that still need to be developed.

  1. Developing the Router/Dispatcher Class for a Simple MVC-based Framework in PHP 5
  2. Review: start building the MVC-based framework
  3. Building the router/dispatcher class
  4. Listing the framework’s partial source code
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
June 08, 2010

print this article



In case you still haven’t had the chance to read the preceding part of this series, which covered the creation of the base “.htaccess” file and the framework’s front controller, below I reintroduced the definitions of these files.

Having said that, first here’s the signature of the “.htaccess” file, which is charged with redirecting all of HTTP requests straight to “index.php”:

# 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]

As I said a moment ago, this file includes the minimal settings required for centralizing user requests through a single file, and nothing else. Of course, it’s possible to add to it more directives in accordance with specific needs, but for this project I’ll keep this file basic. To make this file work, you should make sure that the mod_rewrite module has been enabled on your Apache web server, and that it supports “.htaccess” files in the directory under which the framework will live.

Have you already done this? Great. It’s time to show the definition of the “index.php” file, which  acts like the framework’s front controller:


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

From the source code of the above file, it’s easy to follow all the tasks that it performs. It starts by registering via the “spl_autoload_register()” PHP function the callback method that will be invoked when autoloading classes, then it defines a custom exception class that extends the default Exception class, and finally it  finishes its execution, including the autoloader and the dispatcher classes within a “try-catch” block. That’s about it.

Due to the simple logic implemented by the pertinent “Autoloader” class, I’m not going to waste your time explaining once again how it works. Instead, I’d like you to focus your attention for a moment on the following line within “index.php”:


Here, it’s clear to see that this line is statically calling a “dispatch()” method that belongs to a class called “Dispatcher.” As its name suggests, this class will be responsible for dissecting in pieces the requested URL and routing the request to the proper method of the matched controller, if there are any available.

However, the definition of this router/dispatcher class hasn’t been shown so far. It's time to start coding it, so you can quickly understand its driving logic. I will discuss building that class in the next section, so click on the link below and keep reading. 

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