Modularizing a PHP website
I am about to start building a new PHP/MySQL application. I always use the traditional MVC architecture, however, I am thinking of making a small change with the way I set my application out.
Usually, each of my VIEW files independently calls the header HTML and footer HTML files.
I am thinking what I would like to do is have my entire application run through a single control file (eg, index.php). Each page would then be accessed by passing a GET value in the URL. Eg, instead of accessing certain pages directly, like this:
My site would instead load each page through index.php like this:
What I am trying to decide is whether or not I should have the header and footer include commands in each individual VIEW file, or should I let the index.php file take care of that, meaning each VIEW file will only contain HTML <body> to </body> text?
I'd love to get some insight in to how other people are structuring their applications in this respect.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Well, I use modular structure for my pages, and let a module loader custom build the site for each request.
I have tried to build websites in almost every way in my time, and this is far the best way to do it in my opinion.
But if you wish to keep it simple, and still want to be able to put static content into the main page, but with the ability to insert extra scripts and style sheets in the head section, just create some variables.
and then echo it out where you want it.
Then include your pages in the top of the index page. Now you can just put what ever you want into your variables.
I've done it various ways, but what I find usually works best for me is to have all the pages called from one central script, as you said.
My approach tends to be that the string representing what the client sees "acquires" difference bits depending on various conditions. So, I might do something like this:
require($_POST['page'] . '.php');
<title><?php echo $title; ?></title>
<?php echo $extraheaders; ?>
If($etc) echo $etc;
Last edited by Jerail; 01-25-2010 at 02:51 AM.
Thank you for all the responses. Not quite sure yet which way I'll go yet, but this will help with my thinking.
Personally I favour the MVC approach. There is nothing to stop you using this and just nesting your views. Typically I have 3 layers to my views, a top layer which contains the main site layout, head section, footer etc, then main views which get plugged into one of the top layer views, then an elements layer which is partial blocks of content which can be injected into other views. This gives you enormous flexibility because you can cache views at any of these 3 levels - ie you can fully cache the whole thing for a static page, or plug in cached static content into higher level views etc.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)