PHP / MVC

Associate
Joined
14 Oct 2008
Posts
416
I'm doing a final year project for uni in PHP and I'm currently trying to get my head around the MVC design pattern. I've followed a tutorial on phpit which helped me create a basic MVC system to work from but I'm really struggling to understand how to successfully "split" everything and what is and isn't acceptable.

For example, I'm trying to put all the html for the site in template files so I've got a "header" template which I'm putting at the top of every page (obviously), this should have: "Login | Register" if they're not logged in, and "My Area | Logout" if they're logged in. So...

Code:
html stuff here.....

<?php
if(!isset($isLogged)){
	echo '<a href="login">Login</a> | <a href="register">Register</a>';
}else{
	echo '<a href="myarea">My Area</a> | <a href="logout">Logout</a>';
} 
?>

bit more html stuff.....

That's all in the index template file... is it OK to put php logic like that in the template? Or should that be kept to the controller somehow?

Any help/advice would be appreciated. Also if anyone can recommend any books specifically for MVC or design patterns with PHP which you think may help that'd be great too.
 
For systems that have a lot of pages, I'll tend to break down the template so that it has just the rough outline of the page.

Then I'll have several includes/requires, something like:

common.inc.php (for useful variables), pageheader.inc.php (for header graphics, etc), pagemenu.inc.php (for menu options, login/out), pagefooter.inc.php... and so on. In your example, put the include where you'd expect it to be output within the template, and it'll work perfectly ok.

That's a pretty simplistic way (latest project has around 25 includes and about 60 classes as things stand), but it'll allow you to alter one include and affect the whole site without having to upload an entire site of templated .php files.

OK, I've been told including (a lot) of files isn't good practice? Is this true?

Personally I can't see why it's bad and everyone seems to do it online but this was a reasonably respectable guy.
 
Back
Top Bottom