When the index breaks up the URL there are three different cases:
- The URL is pointing to a real document
- The URL is pointing to the index
- The URL is pointing to a virtual document
The case of the real document is easy: I simply include that document.
The case of the index is a little bit more complicated: I have to output the template and fill it with the front page. The front page is dynamic content and is generated by whichever module is designated as the default module.
The final case is slightly more complicated still: I have to output the template and fill it with the dynamic content as parameterized by the rest of the URL.
The front page is generated by a module as if the module was asked to display content with no parameters so it seems like these two cases can be combined into one. Each module already has functions to generate content so it seems like all I'll have to do is call these in the correct place.
First let's look at the current code that handles these cases
// Does this file exist?if (file_exists($DOCUMENT_ROOT . $REQUEST_URI) and ($SCRIPT_FILENAME != $DOCUMENT_ROOT . $REQUEST_URI) and ($REQUEST_URI != '/')) { // The file exists, simply include it include($DOCUMENT_ROOT . $REQUEST_URI);} else { // The file does not exist, the content might be dynamic $url = strip_tags($REQUEST_URI); $url_array = explode('/', $url); // The first element of the array is empty since the first char is '/' array_shift($url_array); if (empty($url_array)) { // The request was for the index if (DisplayFrontPage() == false) print "Error loading front page"; } else { // The first entry is the module to run, and so we must attempt // to execute that module. The rest of the URL array become the // module's arguments $module = $url_array[0]; array_shift($url_array); ExecuteModule($module, $url_array, GetRequestVars()); }}
ExecuteModule() will execute the module's content-generating function and that function will output the content to the browser so the XHTML code will need to be embedded within this if. Ugly.
This is how I'm planning to reorganize it
---index.php---// A generic function that prints the content of the URLfunction PrintModuleContent() { $url = strip_tags($REQUEST_URI); $url_array = explode('/', $url); // The first element of the array is empty since the first char is '/' array_shift($url_array); if (empty($url_array)) { // The request was for the index if (DisplayFrontPage() == false) print "Error loading front page"; } else { // The first entry is the module to run, and so we must attempt // to execute that module. The rest of the URL array become the // module's arguments $module = $url_array[0]; array_shift($url_array); if (ExecuteModule($module, $url_array, GetRequestVars()) == false) print "Error loading page"; }} // Does this file exist?if (file_exists($DOCUMENT_ROOT . $REQUEST_URI) and ($SCRIPT_FILENAME != $DOCUMENT_ROOT . $REQUEST_URI) and ($REQUEST_URI != '/')) { // The file exists, simply include it include($DOCUMENT_ROOT . $REQUEST_URI);} else { // The file does not exist, the content might be dynamic // Output the template require_once('template.php');}---template.php---.......class="content"> PrintModuleContent();?>...
Now the user is free to make a template that is mostly free from PHP. The PHP parts will probably be labeled as 'Place this code where you want your content to show up'.
There we go! A basic template system.