/    Sign up×
Community /Pin to ProfileBookmark

Modularizing a PHP website

Hi There,

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:

[url]www.somesite.com/about_us.php[/url]
[url]www.somesite.com/faq.php[/url]
[url]www.somesite.com/news_and_events.php[/url]

My site would instead load each page through index.php like this:

[url]www.somesite.com/?p=about_us[/url]
[url]www.somesite.com/?p=faq[/url]
[url]www.somesite.com/?p=news_and_events[/url]

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?

Although having the header and footer include commands contained within the main index.php file seems to be cleaner and more modular, what if I want to include some extra information between the <head></head> tags of only [I]some[/I] view files? Eg, an extra CSS link, or a Javascript link, etc?

I’d love to get some insight in to how other people are structuring their applications in this respect.

Thank you.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 25.2010 — I normally call them in each view, but that is in part because it seems to me the easiest way to work it with the CodeIgniter framework, which is what I normally use. It also allows me to customize some aspects via optional function parameters (e.g. a page-specific javascript or CSS file, page title, etc.). I'm in no way saying that's the best or only way I'd do it, but it works for me.
Copy linkTweet thisAlerts:
@dk_zero-coolJan 25.2010 — 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.

$content

$head_content

etc.

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.
Copy linkTweet thisAlerts:
@JerailJan 25.2010 — 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:
[code=php]
<?php
require($_POST['page'] . '.php');
require('global_headers.php');
require('navigation.php');
?>
<!DOCTYPE...
<html>
<head>
<title><?php echo $title; ?></title>
<?php echo $extraheaders; ?>
</head>
<body><div id="wrapper">
<?php
echo $navigation;
echo $content;
If($etc) echo $etc;
while(blahblahblah>0)...
echo $footer;
?>
</div>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@callumdauthorJan 26.2010 — Thank you for all the responses. Not quite sure yet which way I'll go yet, but this will help with my thinking.
Copy linkTweet thisAlerts:
@MindzaiJan 26.2010 — 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.
×

Success!

Help @callumd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...