tags:

views:

88

answers:

5

Hello there!

I have been working with PHP for a couple of years now but I don't consider myself any more than a moderate programmer.

While creating some websites (varying from presentation websites to some simple CMS'es), I have taken 2 approaches which I will discuss below. My question is simple: which one is better in terms of customization (it needs to be as customizable as possible) and speed (to load as fast as possible).

First choice

files: header.php , content1.php, content2.php , footer.php

idea: all the content files include the header at the beginning of the file and the footer at the end.


Second choice

files: index.php , content1.php, content2.php

idea: based on a GET variable or something similar, index.php includes the relevant php file


Thanks in advance for the answer!

A: 

If the website really is simple and doesn't need any additional utility, i would go with the second choice. Mostly the HTML is not so complicated, that it has to be splitted into several files, the actual presentation magic should rest in the CSS file anyway... With the second choice you have your - simple - website in one place and don't have to look around in several files if you want to edit something.

Tapdingo
A: 

I have a nifty framework that works equally well for small sites as it does large. The structure is pretty much the same in all instances and my directory structure looks as follows:

.htaccess
inc/
tpl/
    css/
    images/
    js/
index.php

The job of index.php is to determine what file to load, where any programming logic is held in files in the inc directory, and templates are help in the tpl directory. For example, index.php could be as simple as this:

<?php
switch ($_GET['filename']) {
    case "news":
        require_once('inc/news.php');      // your news functions
        include_once('tpl/news.tpl.php');  // your news template
    break;
    case "events":
        require_once('inc/events.php');
        include_once('tpl/events.tpl.php');
    break;
    case "contact":
        require_once('inc/contact.php');
        include_once('tpl/contact.tpl.php');
    break;
    default:
        if ($_GET['filename']=="") {
            include_once('tpl/home.tpl.php');
        }
        else {
            include_once('tpl/page_not_found.tpl.php');
        }
    break;
}
?>

Coupled with the following .htaccess rules:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php?filename=$1

Hopefully that makes sense. If not, let me know what doesn't and I'll be happy to explain further.

Martin Bean
+2  A: 

I would go with the 2nd one. This is the approach taken by many frameworks and a very good one. You have a single place that get's always called, so you could do some processing in there like cleaning up the URL, checking for a valid Session,...

Furthermore, you can store some basic options inside the index.php (since it's a small site I see ne problem in this way of doing it but with bigger projects, sperate config files are the better way IMO) that can be accessed by all pages that are getting called (included) from within the index.php file.

Speed should not be an issue with a small site so whatever solution you may choose in the end will have good results.

DrColossos
+1 for -one entry point to your application- it has many advantages (http://stackoverflow.com/questions/604046/having-a-single-entry-point-to-a-website-bad-good-non-issue) .. I'd probably choose a more MVCish way even on small applications tho..
Kuchen
I can only up DrColossos' opinion, since I use it myself. But you have to be careful about something. If you try to set the include depending on a GET parameter, you should make an array of authorized pages, just to protect your website, or try to search if the file exists on the server...
Squ36
i like this solution the most but i am just having some issues in regards with someone accessing one of the included files directly (I'm not really sure how I should make the server react to this attempt)
Victorelu
You can define inside a .htaccess file that you always want to put everything through the index.php and simply display an error/default page if the site was accessed differently.
DrColossos
A: 

Victorelu - not really an answer to your question. But you might be advised to begin looking at some of the lighter weight MVC php frameworks (i'm not talking about CI, kohana, cake etc, more the very lightweight ones such as caffeine). I did a little proof of concept using caffeine (a lightweight MVC framework based on a few core files) that neatly addresses the requirement that you state:

'... customization'.

jim

[edit] - link to caffeine: http://code.google.com/p/caffeine-php/ there's agreat little pdf that walks the entire process: http://code.google.com/p/caffeine-php/downloads/list

jim
Jim, thanks for posting that link. Caffeine looks pretty sweet!
Martin Bean
Martin - yes, i found it a great little app, especially as i'm more asp.net mvc. there were many similarities. in house, we tend to use .net and for the linux sites, we customize joomla, so caffeine is very refreshing after joomla :)
jim
A: 

i didnt use any framework for my projects. but everyone advice me to use a framework. i maintain a file/directory structure of my own

INSIDE ROOT DIR

  • /backups
  • /config
  • /library
  • /media
  • /models
  • /modules
  • /templates
  • /sql files
  • index.php
Jaison Justus