Understanding MVC in PHP
Pages: 1, 2, 3, 4, 5, 6
Presenter.php
This is the foundation for the presentation layer. It uses the factory design pattern to create the presentation layer. FR_Module::$presenter defines which presentation layer to use, which the controller will then create via the factory method. Once the controller has a valid presentation layer, the only thing left to do is to run the common display() function, which presentation classes inherit from FR_Presenter_common.
<?php
class FR_Presenter
{
// {{{ factory($type,FR_Module $module)
/**
* factory
*
* @author Joe Stump <joe@joestump.net>
* @access public
* @param string $type Presentation type (our view)
* @param mixed $module Our module, which the presenter will display
* @return mixed PEAR_Error on failure or a valid presenter
* @static
*/
static public function factory($type,FR_Module $module)
{
$file = FR_BASE_PATH.'/includes/Presenter/'.$type.'.php';
if (include($file)) {
$class = 'FR_Presenter_'.$type;
if (class_exists($class)) {
$presenter = new $class($module);
if ($presenter instanceof FR_Presenter_common) {
return $presenter;
}
return PEAR::raiseError('Invalid presentation class: '.$type);
}
return PEAR::raiseError('Presentation class not found: '.$type);
}
return PEAR::raiseError('Presenter file not found: '.$type);
}
// }}}
}
?>
Up Next
So far I have covered why using MVC frameworks make sense and coded the foundation classes. The next article will cover the controller. The third part will cover the view, or presentation layer. The final part of this series will show the creation of the first application built using the framework, the Model.
Example code for this article: framework-0.2.tar.gz
Joe Stump is the Lead Architect for Digg where he spends his time partitioning data, creating internal services, and ensuring the code frameworks are in working order.
Return to the PHP DevCenter.



It's difficult to found some good explainations or examples for mvc framework mostly when we are beginners in php5.
Thanks to your article, i'm starting to understand the mvc concept. Great job.
When will your publish the following ?
I'm impatient to read you !