try another color:
try another fontsize: 60% 70% 80% 90%
PHP Suit
Useful PHP scripts

MVC framework skeleton - I

How to write your very own PHP MVC framework - part 1: Front controller and .htaccess rules.

My goal is to show how to create simple MVC framework step by step.

Assume simple framework with the file layout simillar to my framework, as described here:

/class/ (...) site-specific classes
/controllers/ (...) site-specific controllers
/pages/ (...) site-specific HTML pages / templates
/images/ (...) images (accessed directly due to .htaccess rules)
/layout/ (...) other site-specific layout files (css, js...)
/.htaccess - basic rules for HTTP requests redirecting
/index.php - the site front controller

.htaccess file

The most important thing is HTTP request redirecting - you've to redirect all requests to front controller index.php instead of directories and files. This work can be done by .htaccess file (on systems based on Apache HTTP server, which is the most used server, with the Mod_Rewrite enabled - consult your hosting's FAQs if you aren't sure).

The .htaccess file should be in the web root directory and it should contains something like:

Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !/images
RewriteRule ^(.*\.(jpg|gif|png))$ /images/$1 [L,PT]
RewriteCond %{REQUEST_URI} !/layout
RewriteRule ^(.*\.(css))$ /layout/$1 [L,PT]
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [L]
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

These rules says that the files *.jpg, *.gif and *.png resides in the /images directory, *.css are in the /layout directory and all other files are handled by the index.php script.

Index.php - front controller

The first thing you have to do in the index.php controller is to parse requested URL and decide what controller/view should be used. The URL routing will be discussed in the next part. But routing isn't the only thing we can do in the front controller.

The things the front controller should do

Here is the list of the things the front controller should do:

  1. Define some common functions and constants
  2. Parse required URL
  3. Make redirtects etd.
  4. Call the controller
  5. Provide the mechanism for views
  6. Deal with all runtime errors

Autoloader

In my index.php the __autoload() magic function is declared - the simpliest autoloader should look like this:

function __autoload($class_name) {
	$class=strtolower($class_name);
	$fn = './class/'.$class . '.php';
	if (file_exists($fn)) {
		require_once($fn); return;
	}
	die('Class '.$class_name.' not found');
}

This autoloader assumes that every required class is in the file with the same name - e.g. class "MediaDirectory" is defined in the "mediadirectory.php" file, which is stored in the /class directory.

Localhost flag

A good idea is to decide if the server is localhost (development) or production. You should make the decision this way:

$localhost = ($_SERVER['SERVER_ADDR'] == '127.0.0.1')? true: false;
define ('LOCALHOST',$localhost, 1);

To be continued...