Okay,
This is just to settle some curiosity, really.
A friend of mine was curious on how Mod-rewrite works, so I had illustrated an example from the Zend Framework:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
Which, from what I have read, will direct anything that isnt a directory, or real file to index.php, and furthermore if I am not mistaken, it is sent through REQUEST_URI (using $_SERVER).
Below is a VERY BASIC example NOT indended for production use.. I just wanted to clarify since it is buggy code that would probably cause a site not to work correctly.
<?php
//index.php
$params = explode('/',strtolower($_SERVER['REQUEST_URI']));
array_shift($params);
printf("<!-- %s -->",print_r($params,true));
$pagename = $params[0];
if (sizeof($params) == 0 || empty($params) || ($params[0] == '' && sizeof($params) == 1)) {
$pagename = "Homepage";
}
echo "You are requesting the main page of: <strong>'$pagename'</strong>. <br>\n";
$paramCount = sizeof($params);
if ($paramCount >= 2) {
if (($paramCount % 2) == 0) { // if its greater than two, but even numbers
$subpage = $params[1];
echo "The Subpage of: <strong>'$subpage'</strong>. <br>\n";
for ($i = 2; $i <= ($paramCount-1); $i++) {
$j = $i+1;
echo ($i % 2) ? "" : "You are trying to send the GET variable <strong>'{$params[$i]}'</strong> with a value of <strong>'{$params[$j]}'</strong>. <br>\n" ;
}
} else {
// uneven number of variables
for ($i = 1; $i <= ($paramCount-1); $i++) {
$j = $i+1;
echo ($i % 2) ? "You are trying to send the GET variable <strong>'{$params[$i]}'</strong> with a value of <strong>'{$params[$j]}'</strong>. <br>\n" : "";
}
}
}
?>
And it does work, and DOES indeed illustrate a very basic unworthy front controller.
My only issue is - at least on the webhost I use (uses php cgi) when I attempt to access a PHP file (or phps for that matter, and yes, its defined in apache config, its a dev site only) it passes it to the index.php, however when I attempt to view a folder, image, css, js file it seems to work (shows that file)
example:
http://site/hello/world
Output:
You are requesting the main page of: 'hello'.
The Subpage of: 'world'.
http://site/hello/world/foo
You are requesting the main page of: 'hello'.
You are trying to send the GET variable 'world' with a value of 'foo'.
etc etc...
http://site/index.phps (which exists)
Output:
You are requesting the main page of: 'index.phps'.
http://site/button.gif (exists)
[image file shown]
This is for the purpose of learning, and perhaps one day, use in some form of production, so any help would be greatly appreciated.