Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Whats the best approach for a simple php websites structure?

Hello everyone, I am currently working on a project with ~20 subpages. I had this structure in index.php to display certain pages:

<?php 
include ('inc/header.php'); 

if ($_GET["page"] == "xyz"){
include ('pages/xyz.html')
}

include ('inc/footer.php'); 
?>

After doing Randys simple php website tutorial i thought about implementing his approach: every page is a xyz.php file with this structure:

<?php 
$pageTitle = "xyz";
include ("inc/header.php");
?>

Html for this site

<?php
include ("inc/footer.php");
?>

What is the best approach for my scenario and why? Do you have any other tips for me?

~ Nikos.

2 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

I haven't really seen anyone do it quite the way you're doing it. It's pretty strange having your web addresses look like this:

The one nice thing about your approach is that you only write the code the header and footer one time instead of duplicating that in every file.

In the second version of the Shirts 4 Mike project, we're changing the web addresses using subfolders and rewrite rules. (The videos about subfolders are live now, and the videos on rewrite rules should be live soon.) I think that's a more standard approach for a site your size.

For larger sites or web applications, I would recommend using something known as the "front controller" pattern. With a sophisticated set of rewrite rules, every web address runs through an index.php. Just like you are doing now, you can then include the header once, the corresponding page, and then the footer. Because it uses rewrite rules, the web addresses can be much cleaner ...

... but they still run through a single index.php file, known as the "front controller".

Does that help?

Yeah, it helps a lot! Thanks Randy!

That was exactly my point, like you always say: "dont duplicate code". I saw this ?page attempt at another tutorial. Guess it was also outdated.

I am looking forward to the next parts of your php series. It really boosted my php understanding.

I also tried out your subfolder-webadresses but in my test-enviroment it just broke cause i didnt have any rights for the root of the webspace. (it was a free webspace service with ads. they didnt build it properly i guess).

I'll try to get it to work in localhost. Something didnt work with the ROOT_PATH and BASE_URL constants. I will get back to the forum with specific questions soon. anyways: thanks for the detailed answer.

Best Regards, Nikos.

PS: teamtreehouses service is really great <3