Reply
[ARTICLE] PHP Maintaining Application Structure
Old 03-04-2008, 01:18 AM [ARTICLE] PHP Maintaining Application Structure
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,552
Name: Keith Marshall
Location: West Hartford, CT
Trades: 0
Maintaining Application Structure

Introduction:
Web development can be difficult enough by itself, and it can be very difficult when you don’t apply some planning and file management in your process. Building an application uses several script files which all work together to help you focus on the primary features of the pages you are building. Maintaining your includes help you separate all types of functions, classes, templates, etc. in an organized fashion that can eliminate the guess work of what script file does what and where they are stored.

I am providing an example of key elements and structure format which may help the majority of developers keep focus on the challenge at hand. There are infinite ways to build an application and many are very creative and thoughtful. Here is just one way of achieving this.

This article also makes use of the mySqlClass

File Structure:
The way you organize your PHP files pretty much defines the way of how your application is built. Planning at this first step will greatly determine how all your includes work, and trying to change this structure later at a more mature point of your entire application and be difficult.

The way this example is shown, all of our application includes reside in a directory network within our public html directory. The key files you will see here are the application_top.php, configure.php and template files.

Code:
- [DIR] public_html
    |
    |- [DIR] images
    |    |
    |    |- logo.gif
    |
    |- [DIR] includes
    |    |
    |    |- [DIR] classes
    |    |    |
    |    |    |- [FILE] mySqlClass.inc.php
    |    |    
    |    |
    |    |- [DIR] functions
    |    |    |
    |    |    |- [FILE] application.functions.php
    |    |
    |    |- [DIR] templates
    |    |    |
    |    |    |- [FILE] template_header.php
    |    |    |- [FILE] template_footer.php
    |    |    |- [FILE] stylesheet.css
    |    |
    |    |- [FILE] application_top.php
    |    |- [FILE] configure.php
    |
    |- [FILE] index.php
    |- [FILE] page2.php


The Configuration File:
We will define a series of static constants that will define the file paths to the application files. The reason why we do this is because if we were to move our application to another serving host, we can easily edit these values and the changes would be changed globally in all our pages.

PHP Code:
<?php
  
  
//## HTTP Server without trasiling slash
  
define('HTTP_SERVER''http://www.domain.com');
  
  
// Path to public webserver (WS) (helpful if you use shared hosting or want to test in a temporary location)
  // This is relative to the public html directory
  
define('DIR_WS_PUBLIC''/');
  
  
// Path to public fileserver (FS)
  // This is the absolute path from the server root
  
define('DIR_FS_PUBLIC''/home/account/domains/domain.com/public_html/');
  
  
// Define the fileserver includes directory path
  
define('DIR_FS_INCLUDES'DIR_FS_PUBLIC 'includes/');
  
  
  
//## Define database connection
  
define('DB_SERVER''localhost');
  
define('DB_SERVER_USERNAME''username');
  
define('DB_SERVER_PASSWORD''password');
  
define('DB_DATABASE''database');


The Application File:
This file can start off your entire application by using just one include on the visited page.

PHP Code:
<?php
  
  
//# Line to add a layer of protection
  
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) die(header('HTTP/1.1 403 Forbidden'));
  
  
  
//# Load configuration file
  
require('configure.php');
  
  
  
//# Auto include function files
  
if ($dir = @dir(DIR_FS_INCLUDES 'functions/'))
  {
    while (
$file $dir->read())
    {
      if (
strstr($file'.php'))
      {
        require(
DIR_FS_INCLUDES 'functions/' $file);
      }
    }
  }
  
  
  
//# Set current page (or self) as a constant
  
define('PHP_SELF'$_SERVER['PHP_SELF']);
  
  
  
//# Start database connection
  
require(DIR_FS_INCLUDES 'classes/mySqlClass.inc.php');
  
$db = new mySqlClass();
  
$db->Connect(DB_SERVERDB_SERVER_USERNAMEDB_SERVER_PASSWORDDB_DATABASE);
  
  
  
//# Start session
  
session_set_cookie_params(0DIR_WS_PUBLIC);
  
session_start();


The HTML Template Files:
Here is where you can easily include your design template to be included throughout each page you build. The reason you want to have these types of files separate from the application includes is because you can include these only at the point when its ready to start output buffering. You could also include another set of template files if desired as well.

template_header.php

PHP Code:
<?php
  
  
//# Line to add a layer of protection
  
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) die(header('HTTP/1.1 403 Forbidden'));
  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title><?php if(defined('PAGE_TITLE') AND !empty(PAGE_TITLE)) echo PAGE_TITLE ' :: '?>My Wonderful Application</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link href="stylesheet.php" rel="stylesheet" type="text/css" title="default" />
<?php
  
  
if (defined('PAGE_AUTHOR') AND !empty(PAGE_AUTHOR)) echo '    <meta name="author" content="' PAGE_AUTHOR '" />' "\n";
  if (
defined('PAGE_DEFINITION') AND !empty(PAGE_DEFINITION)) echo '    <meta name="description" content="' PAGE_DEFINITION '" />' "\n";
  if (
defined('PAGE_KEYWORDS') AND !empty(PAGE_KEYWORDS)) echo '    <meta name="keywords" content="' PAGE_KEYWORDS '" />' "\n";
  
?>
  </head>
  <body<?php if(defined('BODY_ONLOAD') AND !empty(BODY_ONLOAD)) echo ' onload="' BODY_ONLOAD '"'?>>
  
  <!-- Header HTML Goes Here //-->


template_footer.php

PHP Code:
<?php
  
  
//# Line to add a layer of protection
  
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) die(header('HTTP/1.1 403 Forbidden'));
  
?>
  
  <!-- Footer HTML Goes Here //-->
  
  </body>
</html>


Building The Main Page:
The “main” page I refer to is the actual visited page as defined in the URL of the visitors browser. For example, the home page would be index.php. This is the main script page in which I would build the main functionality of that specific page. I will include the application file which will auto start my entire application engine for the entire execution cycle.

So here is a simple template structure for the index.php page:

PHP Code:
<?php
  
  
//# Include application relative to the location of this page
  
require('includes/application_top.php');
  
  
///////////////////////////////////////////////////////////////////////////////
//###  Processing section:
  
  // This is where you would parse form data before output is sent to browser
  // You are safe here to send custom headers if needed
  // such as redirect
  
  
  
///////////////////////////////////////////////////////////////////////////////
//###  Page Content section:
  
  
require(DIR_FS_INCLUDES 'templates/template_header.php');
  
?>
 
    Main Page Content HTML
 
<?php
  
  
require(DIR_FS_INCLUDES 'templates/template_footer.php');
  
?>


Overview:
I hope this article helps you to achieve more manageable methods of development and building an application. This is intended to provide a learning tool for those who are interested in learning more about PHP and application development. The provided code is only for suggestion, and those who would like to offer suggestions and improvements are encouraged to do so.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 03-04-2008, 01:23 AM Re: [ARTICLE] PHP Maintaining Application Structure
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
Converting Video For YouTube
Posts: 2,552
Name: Keith Marshall
Location: West Hartford, CT
Trades: 0
I forgot to add in the header defines in the index template:

PHP Code:
<?php
  
  
//# Include application relative to the location of this page
  
require('includes/application_top.php');
  
  
///////////////////////////////////////////////////////////////////////////////
//###  Processing section:
  
  // This is where you would parse form data before output is sent to browser
  // You are safe here to send custom headers if needed
  // such as redirect
  
  
///////////////////////////////////////////////////////////////////////////////
//###  Define header detials:
  
    
define('PAGE_TITLE''Home');
    
define('PAGE_AUTHOR''mgraphic');
    
define('PAGE_DEFINITION''');
    
define('PAGE_KEYWORDS''');
    
define('BODY_ONLOAD''');
  
  
  
///////////////////////////////////////////////////////////////////////////////
//###  Page Content section:
  
  
require(DIR_FS_INCLUDES 'templates/template_header.php');
  
?>
 
    Main Page Content HTML
 
<?php
  
  
require(DIR_FS_INCLUDES 'templates/template_footer.php');
  
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to [ARTICLE] PHP Maintaining Application Structure
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.13463 seconds with 13 queries