I personally use the following method for all my sites. It has grown a little bit from using prefixed $_GET variables to using mod__rewrite but i have had these basics for a couple of years now.
.htaccess:
RewriteEngine On
RewriteRule ^includes/ - [L] [OR] #do not apply for direct requests to /includes or
RewriteRule ^images/ - [L] [OR] #do not apply for direct requests to /images
RewriteRule ^(([^/]+)/){0,}([^/]+)?$ index.php #rewrite all requests to index.php
index.php
*this file is obviously simplified, flattened and stripped to it's bare bones. All the basics are the same though: be super-flexible and have as little overhead as possible.
<?php
session_start();
global $_TPL, $_URI; #$_URI will hold all the request parts. $_TPL will hold title, scripts, body. menu, custom page javascript, whatever you want.
error_reporting(E_ALL);
/* figure out the base url to index.php no matter where you place this script. could be in the root of a site, or even in some 2-level subdir */
$basedir = 'http://'.$_SERVER['HTTP_HOST'].(dirname($_SERVER['SCRIPT_NAME']) != '/' ? dirname($_SERVER["SCRIPT_NAME"]).'/' : '/');
/* convert URI to lower case, strip the basedir, drop empty parts and explode it to an array */
$uri = explode('/', str_replace(strtolower($baseDir), '', urldecode(strtolower('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))));
foreach($uri as $key=>$val)
{
if(!empty($val)) $_URI[] = $val;
}
/* now $_URI contains all url parts. we can load the plugins: */
function LoadPlugins($dirname)
{
global $_TPL, $db;
$files = glob("{$dirname}*.php");
foreach ($files as $currentfile)
{
require($currentfile);
}
}
LoadPlugins('./plugins/'); // this will just include all the files in the plugin dir.
include './includes/template.inc.php';
?>
Each plugin in ./plugins/ looks like this:
<?php
global $_TPL, $_URI;
switch($_URI[0])
{
case 'comment':
switch ($_URI[1])
{
case 'submit':
/*
do your action for http://yoursite/comment/submit here.
Maybe create some objects,etc. When you're done, either die() with some html
if you run an ajax request, or let the loop continue so that the next plugins will
be loaded and we'll eventually land back @ index.php where the
template can be included.
*/
$_TPL['body'] .= "you have submitted a new comment";
break;
case 'delete':
/* this is one of my typical ajax actions (obviously simplified and without checks)
normally i also have an __autoload() function available that handles class loading.
*/
$comment = new comment($_POST['id']);
$comment->deleteYourself();
die("comment {$_POST['id']} deleted.");
break;
default:
/* unknown action for /comment/ */
die("wtf are you trying?");
break;
}
break;
}
?>
And finally: template.inc.php looks like this (i'm a huge fan of the 'PHP IS the template language' mantra)
<?php global $_TPL; ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?=$_TPL['title'];?></title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<?
if (!empty($_TPL['baseDir']))
{
echo ("<base href='{$_TPL['baseDir']}' >\n");
}
if (!empty($_TPL['scripts']))
{
$_TPL['scripts'] = array_unique($_TPL['scripts']);
for ($i=0; $i<sizeof($_TPL['scripts']);$i++)
{
echo('<script type="text/javascript" src="'.$_TPL['scripts'][$i].'"></script>'."\n");
}
}
if(!empty($_TPL['styles'])) {
foreach($_TPL['styles'] as $css)
{
echo("<link rel='stylesheet' type='text/css' href='{$css}'>");
}
}
?>
<?=((!empty($_TPL['style'])) ? '<style type="text/css">'."\n".$_TPL['style']."\n</style>" : '');?>
<?=((!empty($_TPL['script'])) ? '<script language="javascript">'."\n".$_TPL['script']."\n</script>" : '');?>
</head>
<body>
<div id="maincontent" class="<?=$_TPL['bodyclass'];?>">
<?=$_TPL['body'];?>
</div>
</body>
</html>
There you have it. my plugin-based, 'kinda-mvc' lightweight base php setup. The stuff i thow into a directory everytime i start a new site.
There are no tight rules this system forces you to follow, but it does hand you a super simple default setup with everything you need available.
I have thought about a modification where each of the plugins is actually a class, but that would probaly have little to no added functionality,you can just write any initialisation code you need in the index.php before loadPLugins.
Now i'm curious: What are the bare basics you put down when you start a new PHP project?
Please no answers like 'i use cakephp'. It would be nice if everybody could go into a little more detail than that, also describing what you need to do for like a hello world.