The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

dynamic web dev with PHP

I am a PHP newbie and I am trying to figure out how to structure my website.

There are a lot of common elements to the pages -such as header, footer, etc- which I made into separate PHP files. I simply include them with each page.

The main issue I have is how to deal with the very many pages I need to display. Firstly, should each page me a separate file? Or should there be a single file that handles each page based on some parameter in the URL and how the user interacts with the current page? How does WordPress handle this? Any tutorials I could read?

I tried googling but I don't even really know what to google for. PHP tutorials don't seem to deal with this aspect of the design. Any help would be greatly appreciated!
PHP_newbie
Tuesday, February 27, 2007
 
 
"Firstly, should each page me a separate file?"

Assuming that each page is actually different, then yes, I'd go with this approach.  If your application is blog, that doesn't mean making a new PHP page for each blog item.  Instead you have one PHP page that loads the blog item from the database and displays it based on a URL parameter.
Almost H. Anonymous Send private email
Tuesday, February 27, 2007
 
 
>>Or should there be a single file that handles each page based on some parameter in the URL and how the user interacts with the current page?

Yes, otherwise it gets difficult setting cookies or handling redirects.
Tony Edgecombe
Wednesday, February 28, 2007
 
 
I am facing the same issues....

Let's suppose you have only one page that displays different content according to a parameter in the URL : couldn't that be a problem with search engines?

If I have

mypage.php?show=latestnews
mypage.php?show=coolphotos

what is Google going to index? Will it do the right thing?

I suppose it depends on what kind of website you're building, but in my case I want each of my pages indexed. For this reason I have chosen to make separate pages... not sure it's the right solution though!
Parisian Developer (still looking)
Wednesday, February 28, 2007
 
 
The design pattern for what you're describing is Model-View-Controller (MVC).

The url/parameter issue can easy be solved via ModRewrite.
KC Send private email
Wednesday, February 28, 2007
 
 
"what is Google going to index? Will it do the right thing?"

Yes.  Google indexes through query parameters (otherwise, it would never index this forum, for example).

However, I'd argue that the logic behind this...

mypage.php?show=latestnews
mypage.php?show=coolphotos

...is flawed.  At some point you have to have different code for "latestnews" and "coolphotos" which are likely in different files.  It doesn't make much sense to go through that extra complexity when you could just have...

latestnews.php
coolphotos.php

...with the appropriate logic in the appropriate files.
Almost H. Anonymous Send private email
Wednesday, February 28, 2007
 
 
Well I was thinking the reason one might do this would be to have a common template for every page;

mypage.php would be something like

OutputStandardHeader();
switch( show )
 case 'latestnews': OutputLatestNews(); break;
 case 'coolphotos': OutputCoolPhotos(); break;
}
OutputStandardFooter();

That way, the news and the photos page would benefit from the same template via the OutputStandardHeader & Footer functions.

But like I said, I don't do that, instead I do

template.html:
<html><header>blahblah</header>
<etc, etc>
<?php echo $content ?>
<rest of the tags></html>

latestnews.php:
$content = LatestNewsContent();
include( 'template.html');
function LatestNewsContent()
{
  $retval = "<tags>, etc";
  $retval .= "<more tags>, etc";
  return $retval;
}

I like this better because my html template is all in one place, instead of spread out in OutputStandardXxx functions.

Do you know of an even better way?
The only worry I have is that $content might end up being a very long string and I wonder if that might be a problem
for the php engine....
Parisian Developer (still looking)
Wednesday, February 28, 2007
 
 
"Well I was thinking the reason one might do this would be to have a common template for every page"

Well you can have that with either technique.  Just include the common header and footer into each page.

latestnews.php:

include('common/header.php');
// do stuff here
include('common/footer.php');

"But like I said, I don't do that, instead I do"

That seems like a very classic ASP technique.  The LatestNewsContent() function is pretty ugly.  Mixing HTML with code (especially as quoted strings) is a recipe for disaster.

It seems like a lot extra complication for very little gain.
Almost H. Anonymous Send private email
Wednesday, February 28, 2007
 
 
I agree that it's ugly, but then, every solution I've tried so far is ugly in its own way!

And in your case, that would probably the "do stuff here" part. Won't you be "mixing HTML with code", in there ?

Also, your header.php opens tags that will be closed in another file (footer.php), and I find that pretty ugly too...

I would really like to find a definitively elegant way to do all that, but right now I am not getting anywhere near it!
I've read a few books and didn't find anything I liked.
Parisian Developer (still looking)
Wednesday, February 28, 2007
 
 
I prefer this approach:

Use two files for each page: a script and an HTML page. The script handles the logic and data manipluation, the HTML page handles the display. You can, of course, use one script to display more than one page, if it makes sense.

Write a function or class that's responsible for displaying the page. This function will need to have access to any variables that are used by the page; so you'll need to either make them globals (not recommended) or put them into some sort of array that the function can access. This is becase when you call include() from the function, the page you include will have the same variable scope as the function.

But don't include the page directly; include another file that acts as a template. It can contain the basic HTML tags, header, footer, etc. Then from that template, include() your actual HTML page. This will probably be in a variable that you passed into your function. As a very simple example:

news.php:
  include("library.php");
  [load news from database]
  loadPage("news.html", $data)

library.php:
  function loadPage($page, $data) {
    include("template.html");
  }

template.html:
  <html>
    [header]
    include($page);
    [footer]
  </html>

news.html:
  Latest News:
  <?= $data["headlines"] ?>
JW
Wednesday, February 28, 2007
 
 
It looks like I am headed in JW's idea's direction.
PHP_newbie
Wednesday, February 28, 2007
 
 
Thanks JW... I will give it a try!
Parisian Developer (still looking)
Thursday, March 01, 2007
 
 
Some tips:

- Consider Smarty (or another templating language), not because you need it, but it will help you develop good habits.

- Consider using the directory name---*not* the file name---to identify functions. Then use default.php to prepare the content for that page. This lets your URLs be user-friendly:

  www.mywebsite.com/photos/
    not
  www.mywebsite.com/photos.php

- Don't ever let a page accept more than one kind of HTTP request. Pages called default.php should just be for GETs. If you POST to a page, redirect elsewhere with an HTTP Header, so there's no possible way someone could get back to a page with an unclear state.
Robby Slaughter Send private email
Thursday, March 01, 2007
 
 
Thanks for the tips Robby
"Consider using the directory name---*not* the file name---to identify functions"
Does this means that you have to have one directory for each page, each containing his own default.php?
Parisian Developer (still looking)
Friday, March 02, 2007
 
 
Of course.... mod_rewrite!
Forgot about that one! (I'm kinda new to all this)
Thanks for the links, very helpful
Parisian Developer (still looking)
Friday, March 02, 2007
 
 
Parisian---sure, that's one easy way to do it. It creates a somewhat large directory structure, but it's very portable and doesn't require webserver configuration.

Of course, you can do the same thing by rewriting URLs at the webserver level. This is fine, but somewhat contrary to almost every introductory PHP example so I would encourage the OP to stay away from it.
Robby Slaughter Send private email
Friday, March 02, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz