Log in

View Full Version : Revolutionary PHP Site Architecture



Shotgun Ninja
11-19-2007, 04:46 PM
A while back, as some of you might remember, I tried getting phpBB to work. It failed.

Anyways, recently I have been appointed by my school's Robotics Team to build a website. Remembering my experiences with trying to get phpBB to work, and realizing subsequently that I don't even like the layout, I set out to design my own. The result: Fully functional pages that are only about 7 lines long.

All of the major utility functions are segregated to a "scripts.php" file, which is included in all main pages. An example page would be:


<?php

include("scripts.php");

writeHead();

writeTitle("Lakeview Legends - Index Page");

loadPageContent("index.txt");

finishPage();

?>

And that is all you need. The functions writeHead, writeTitle, loadPageContent, and finishPage are all contained in scripts.php. The loadPageContent function looks like this:


function loadPageContent($contentFile)
{
if (!file_exists($contentFile))
{
echo('File not found: $contentFile');
return;
}
if ($file = fopen($contentFile, "r"))
{
while(!feof($file))
{
$line = fgets($file);
echo($line);
}
}
}

I also have a nifty function for embedding a Flash control in PHP without having to rewrite the Flash control every time you make a new one.


function echoFlashControl($flash, $altFile, $width, $height)
{
if (!file_exists($flash))
{
if (file_exists($altFile))
{
if ($file = fopen($altFile))
{
while(!feof($file))
{
$line = fgets($file);
echo($line);
}
}
fclose($file);
}
else
{
echo('ERROR');
return;
}
}
else
{
echo('<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="$width" height="$height" />' .
'\n<param name="movie" value="$flash" />\n<param name="quality" value="high">\n<embed src="$flash" quality="high" ' .
'pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="$width" height="$height" /></embed>\n<noembed>');
if (file_exists($altFile))
{
if ($file = fopen($altFile))
{
while(!feof($file))
{
$line = fgets($file);
echo($line);
}
}
fclose($file);
}
echo('</noembed></object>');
}
return true;
}

Also, here's the writeHead and writeTitle functions.


function writeHead( )
{
echo( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' .
'<link rel="stylesheet" type="text/css" href="style.css" />\n' .
'<html xmlns="http://www.w3.org/1999/xhtml">\n' .
'<head>\n' .
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n' .
'<!-- Begin writing of PHP scripts here -->\n');
}

function writeTitle($content)
{
echo('<title>$content</title></head>\n<body>\n<div class="content">\n');
}

It's not much in the way of revolution, but if XHTML can be standardized and simplified, then so can PHP.

djr33
11-19-2007, 07:14 PM
Not to discredit your efforts, and in fact to verify them, this is a standard practice in PHP, and a very good idea.

In fact, beyond just functions that write content, those this can be useful, and falls under various types of templating-- in fact there may be some other ways, too. One method is to write a skin, which includes variables at several locations; this is written at the end of processing, and values are set through the processing of the script.


The best idea, as I have yet explored at least, is to create a main page which interprets and gathers user input, including especially get variables or mod rewritten URIs.

That page then has a function or series of functions that interpret this and process the correct page, through includes and functions.

This main page also may have login functions (or at least require them through includes), etc., and, depending on the setup of the website, some form of templating.

The included pages each serve a specific purpose as a "page" (in the sense of html) for the site-- one is your search page, the other the info page and the other is your forum, etc.

Each script may be more complex as well, but basically, that's the hierarchy.


To further streamline, functions are used.

I like to include functions.php, which is a general collection of commonly used functions. I include this near the top (before any includes) on the main framework page.

Within functions.php, for organization, I may actually structure is as just a series on includes for various functions-- funcs/login.php, funcs/images.php, funcs/math.php, etc.

Also, since some functions may only relate to one of the 'pages', I will then include a more limited set of functions for just that, ie 'infopagefunctions.php', etc.

It's pretty easy to distinguish between what should be a main function and a limited function.

General system stuff (anything used on the main page, like the login), and repetitively used functions that are useful for more than one 'page' go in the main functions list. The most important ones are also the math functions and string parsing functions, etc., that are basically extensions to the php default functions, not specific to your site. (I've got a handful that I like to have around, like one that deletes a directory and all its contents, and one that allows a number to be raised to a negative power, and a few doing things for arrays.)

Then other functions that are only used for specific pages just go in those other included functions scripts.

Twey
11-20-2007, 03:12 AM
You might want to look at Smarty (http://smarty.php.net/) to help you separate code from content.

Shotgun Ninja
11-20-2007, 03:38 PM
Yeah. Well, it may not have been the revolution I was originally thinking of, but whatever. I'm pretty new to PHP, actually.