Efficient Web Development with PHP

I recently began to develop an OO (Object Orientated) framework to base all of my client’s web sites on. While planning, I tried to write down as many problems that constantly occur within everyday web design and ways to solve them.

Here are a few of the problems I have crossed on more than one location.

  1. Duplicating Template code
  2. Relative Vs Absolute URLS
  3. Search Engine Friendly URLS
  4. Having only one entry point to the site

Duplicating Template Code

You only have to have built a web site with more than a couple of pages and you know it becomes a tremendous hassle when you would like to update a menu item, or change something site wide. A great way to solve the problem and an almost perfect solution for small web sites would be to use the PHP “include” function to simply include the generic file into each page.

Example

Take any content used throughout your site and move it into files named similar to header.inc.php or footer.inc.php.

From here replace exactly where you removed the content from, including the newly created file.

This will include the file at the time the page is requested from the server and will allow you to change the generic content in only one place.

This solves one problem but creates another, within our header we may want to include scripts and CSS files relative to the page’s location. If we were to have pages in many sub directories then the relative URLS in our include files would be wrong and therefore style sheets and scripts would not be included. This would also happen for images that we linked to from within our include files.

Relative Vs Absolute URLS

A solution to this problem would be to have all our URLS absolute and therefore all our links to images, scripts and style sheets would work but only on our server, if we was developing locally this would be another problem.

The simplest way to get around this while you are trying to keep your website simple would be a simple bit of PHP to set a BASEURL. I usually put this code in a file called config.php and then include it within my header, this way I can set the development variable once and we do not have to change the development variable to false every time I re-upload my header file to the server.

$development = true;
if ($development) 
{
    define("BASEURL", "http://localhost/SubFolder", true);
    error_reporting(E_ALL);
}
else 
{
    define("BASEURL", $_SERVER['SERVER_NAME'], true);
    error_reporting(0);
} 
?>

I am also switching all error messages off for my live version of the site.
Now when linking to your images, scripts or even other pages on your site in your menu file you can concatenate the file name to your BASEURL like so.

Ok so we have talked about the first two problems I mentioned, but this is a very simple example and over the next few articles I will talk about some extra features including having a page class as a base template which we can extend, search engine friendly URLS using mod-rewrite, using only one entry point and an in depth look at database classes.

Finally I will bring it all together and release the source code for all my readers.

If anyone has some suggestions of what application they would like to see in the final application based on an OO framework please comment and let me know.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.