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

Defining the framework’s front controller - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I said in the section that you just read, the framework’s front controller will be responsible for performing a few simple tasks, such as autoloading classes on request, invoking the router/dispatcher module, and handling exceptions that may be thrown by other components.

Based on these requisites, the initial definition of the “index.php” file (read the front controller), will look as follows:

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

                                {

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

                                                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.');

                                }

                }

}

As seen above, the partial definition of the front controller is very easy to follow. First, it uses the group of PHP functions included with the SPL library to register in the stack the class (and its corresponding method) responsible for autoloading requested classes.

Once the registering process is complete, the controller includes the definition of the autoloader class, which not surprisingly has been called “Autoloader” as well. Basically, all that the “load()” method of this class does is include a specified class only when required, and throw the corresponding exceptions if the process fails for some reason.

At this stage, the front controller of this simple MVC-based framework is definitely starting to take shape, since it’s been provided with autoloading and basic exception handling capabilities. So, what’s the next step? Well, despite the aforementioned abilities, in its current state the front controller isn’t very useful. It's not capable of evaluating a given HTTP request and dispatching it to the appropriate action controller.

While it’s fair to note that performing these specific tasks will be a responsibility of the dispatcher component, which logically hasn’t been coded yet, it’s necessary to add, within the front controller, the code that bootstraps this dispatcher module.

That’s exactly what I’m going to do in the section to come. So click on the link shown below and read the following segment.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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