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.
 
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.
 
Wouldn't a better approach be something like...

PHP:
<?php
/* assumed $profileLink is already defined and contains members title and url depending on whether user logged in */
?>
<a href="<?=$profileLink->first->url?>"><?=$profileLink->first->title?></a> | <a href="<?=$profileLink->second->url?>"><?=$profileLink->second->title?>

You could probably use a loop instead if you wanted a variable amount of things to show. (foreach $link in $profileLinks)

I will point out that I don't entirely grasp MVC, and I'm in the process of leaning the Zend framework to sort that out (I know I should write my own ;)) . Here's how I have my sidebar set-up in it (which is probably not the best way, btw):

PHP:
<?php
// Sidebar view
?>
<?php foreach($this->menuitems as $key=>$menucategory) : ?>
<fieldset class="sidebarmenuitem">
	<legend><?php echo $key ?></legend>
	<?php foreach($menucategory as $menuitem) : ?>
		<p><a href="<?php echo $this->url(array('controller'=>"{$menuitem['url']}", 'action'=>"{$menuitem['action']}"), null, true);?>" title="<?php echo $this->escape($menuitem['title']) ?>"><?php echo $this->escape($menuitem['title']) ?></a></p>
	<? endforeach ?>
</fieldset>	
<? endforeach ?>


<?
// Populated statically VIA this:
$this->view->menuitems = array(
	"Main Menu"=>array(
		$loginlogout,
		array('title'=>'Contact', 'url'=>'contact', 'action'=>'')),

	"Content"=>array(
		array('title'=>'View Items', 'url'=>'', 'action'=>''),
		array('title'=>'Add New Item', 'url'=>'index', 'action'=>'add'))					
);
?>
 
Back
Top Bottom