[Phpwm] Site Structure

David Goodwin david at codepoets.co.uk
Fri Dec 9 13:54:49 GMT 2005


Matt Harris wrote :
> Thanks for your reply David.
>   
>   With the method you described using the front controller. How would I  redirect users? As there would be output before the PHP which would  cause an error with the header() wouldn't it?
>   
>   Thanks again for you advice.
>   

The front controller would be the gateway to your website. To enforce
this you could do something like :

if(!defined(IN_MYAPP)) {
	die("Get lost");
}
in the top of all files, aside from the index.php one.

index.php could have :
<?php
 /* I think this is how define's work, I may be wrong */
define(IN_MYAPP, "yes");

include("header.php");
$found=false;
if(isset($_GET['page'])) {
 $desired_page = $_GET['page'];
 $allowable = array("index", "aboutme", "foo", "bar", "something", "else");
 foreach($allowable as $ok) {
  if($ok == $desired_page) {
   include($ok);
   $found=true;
   continue;
  }
 }
}
if(!$found) {
    // include default content.
}
include("footer.php");
?> 

The only problem with the above, is that as you rightly said, once
the header is included, if it writes out to the page, then it's not
possible to do any sort of session management or header stuff.

One way aronnd this would be to do something like :

index.php :

<?php
 /* I think this is how define's work, I may be wrong */
define(IN_MYAPP, "yes");

$found=false;
if(isset($_GET['page'])) {
 $desired_page = $_GET['page'];
 $allowable = array("index", "aboutme", "foo", "bar", "something", "else");
 foreach($allowable as $ok) {
  if($ok == $desired_page) {
   include($ok);
   $found=true;
   continue;
  }
 }
}
if(!$found) {
    // include default content.
}
include("header.php");
echo $content; /* <--- NEW BIT */
include("footer.php");
?> 

and $randomPage looks something like :
<?php
if(!defined(IN_MYAPP)) {
	die("Get lost");
}
$content = "A long string full of html for this page...\n";
?>

Which would work...... but you might dislike it.


I'm fairly sure there is a better approach, using something like Smarty
templates, but I've not got enough experience of them to say anything,
yet. (I'm probably safe in assuming that Smarty provides some sort of
include mechansim). Hopefully someone else here can clarify (or suggest
a better approach).

David.
-- 
David Goodwin 

[ david at codepoets dot co dot uk ]
[ http://www.codepoets.co.uk       ]



More information about the Phpwm mailing list