Results 1 to 4 of 4

Thread: Revolutionary PHP Site Architecture

  1. #1
    Join Date
    Oct 2006
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Revolutionary PHP Site Architecture

    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 Code:
    <?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:
    PHP Code:
    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.
    PHP Code:
    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.
    PHP Code:
    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.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You might want to look at Smarty to help you separate code from content.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Oct 2006
    Posts
    110
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yeah. Well, it may not have been the revolution I was originally thinking of, but whatever. I'm pretty new to PHP, actually.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •