3

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!

6 Answers6

10

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 held in the tpl directory. For example, index.php could be as simple as this:

<?php

switch ($_GET['filename']) {
    case 'news':
        require 'inc/news.php';     // your news functions
        include 'tpl/news.tpl.php'; // your news template
        break;

    case 'events':
        require 'inc/events.php';
        include 'tpl/events.tpl.php';
        break;

    case 'contact':
        require 'inc/contact.php';
        include 'tpl/contact.tpl.php';
        break;

    default:
        if ($_GET['filename'] == '') {
            include 'tpl/home.tpl.php';
        } else {
            header('HTTP/1.0 404 Not Found');
            include '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
  • 38,379
  • 25
  • 128
  • 201
  • This is really helpful to me as a beginning web developer. Could you provide a bit deeper insight as how to you are handling the inc/ and the tpl/? I'm guessing the TPLs contain the actual html and the inc/ has just php logic to populate the variables? I will be adopting your structure as I find it very easy to remember. Thanks! – errorprone Feb 06 '13 at 09:02
  • 1
    Yes. Files in the **inc** (includes) directory contain logic, and then variables are “spat” into a mainly-HTML template from the **tpl** directory. – Martin Bean Feb 06 '13 at 11:16
  • Update in 2023: this approach may not be as SEO-friendly as using an _actual_ framework like Symfony or Laravel, given “pages” are determined using a query string parameter rather than “proper” URI paths. You also don’t want PHP code in a publicly-accessible directory. – Martin Bean Jan 11 '23 at 14:02
4

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
  • 12,656
  • 3
  • 46
  • 67
  • +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 Jul 12 '10 at 12:51
  • 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... – 3rgo Jul 12 '10 at 13:34
  • 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) – Victor Avasiloaei Jul 12 '10 at 15:51
  • 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 Jul 12 '10 at 16:23
1

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
  • 2,753
  • 8
  • 47
  • 65
0

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
  • 259
  • 1
  • 2
0

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 tollan
  • 22,305
  • 4
  • 49
  • 63
  • Jim, thanks for posting that link. Caffeine looks pretty sweet! – Martin Bean Jul 13 '10 at 13:39
  • 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 tollan Jul 13 '10 at 14:03
0

Check mofe information about website page structure.

The most important and very basic part of php website is page structure of php website because it help when you want to redesign website or you want to edit some code then sure you need to know page structure of website.

Check full detail here