Apache ReWrites and a simple MVC(ish) Framework.
Simply put, an MVC framework is a way of separating business logic, input logic, and presentation layers in your application. Why is this a good idea?. Well it allows you to focus on the individual elements and test specific parts of the application more robustly.
There are plenty of great MVC frameworks out there, Zend, Symfony and Yii to name a few. The problem for anyone new to PHP is that is very easy to learn the framework as PHP, and become so absolutely dependant on the framework even the little things in PHP become bloat. I often use little PHP scripts for system admin stuff, and the thought of loading an entire framework is not on the cards.
I am a firm believer that every developer should have a go at doing their own framework from scratch. I have come across developers using CakePHP, Zend and other Frameworks that literally have never used anything else and get completely stuck looking at projects that don’t use those frameworks. It’s also very naive to think that one framework will work for all situations.
So developers lets have a go, and create a very simple one!
Here is an .htaccess file in the root document directory.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php [QSA,L]
This basically looks at every request, looks to see if the file exists, if it doesn’t then your directed to the index.php file.
The downsides of this are, it’s slow considering the web server is checking the files existing all the time and you have to
handle your own 404 (File not found) messages.
Since our index.php is handling everything thats requested, we need some code in place to look at the URL and execute
certain bits of code depending on whats been requested.
Here is the "Master and Controller" bit
<?php
class framework
{
public function doit()
{
// Take the URL and Split it up!
$urlElements = explode("/",$_SERVER['REQUEST_URI']);
$url = array();
foreach($urlElements as $k) if (trim($k) != "") $url[] = $k;
$clsModule = $url[1];
$method = $url[2];
$parameter = $url[3];
$parameter2 = $url[4];
// Test for all the Framework bits...
if (class_exists($clsModule))
$objModule = new $clsModule;
else
die("Error loading Class '$clsModule', is it defined?");
if (method_exists($objModule,$method))
$objModule->$method($parameter,$parameter2);
else
die("Error Executing Method '$method' on Class '$clsModule', is it defined?");
if (file_exists($clsModule . "." . $method . ".php"))
require($clsModule . "." . $method . ".php");
else
die("View for $clsModule.$method not found");
}
}
class test
{
public function index($p = null,$p2 = null)
{
var_dump($p);
var_dump($p2);
}
}
$url = framework::doit();
?>
and finally the view
<html>
<body>
View
</body>
</html>
So now, we have a bunch of code that will take all sorts of urls and put them through one main bit of code in a very crude way, so that only specific objects are used for specific urls. Once this object code is done, the view is displayed by a simple require.
Thats your 10 minute framework!!!
Download the source code here