Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file structure for a simple website?

Tags:

php

structure

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!

like image 311
Victor Avasiloaei Avatar asked Jun 03 '26 17:06

Victor Avasiloaei


2 Answers

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.

like image 194
Martin Bean Avatar answered Jun 07 '26 12:06

Martin Bean


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.

like image 42
DrColossos Avatar answered Jun 07 '26 12:06

DrColossos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!