Creating a Web Page Controller with the HMVC Design Pattern - The sample application's full source code
(Page 4 of 4 )
As I promised in the preceding segment, below you'll find all of the source files that comprise the previous example HMVC-based web application, so you can study them more closely.
First, the SQL files that create the corresponding database schema:
DROP TABLE IF EXISTS `test`.`links`;
CREATE TABLE `test`.`links` (
`id` INT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
`href` VARCHAR(45) NOT NULL,
`text` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test`.`news`;
CREATE TABLE `test`.`news` (
`id` INT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(45) DEFAULT NULL,
`text` TEXT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Next, the definitions of the model classes that interact with the previous MySQL tables:
(LinkModel.php)
<?php
// define LinkModel class as subclass of Model
class LinkModel extends Model{}
// End LinkModel class
(NewsModel.php)
<?php
// define NewsModel class as subclass of Model
class NewsModel extends Model{}
// End NewsModel class
Having shown the models, it's time to list the set of controllers. So, here they are:
(LinksController.php)
<?php
class LinksController
{
private $_linkModel = NUll;
public function __construct()
{
// create instance of LinksModel class
$this->_linkModel = LinkModel::factory('links');
}
// fetch all the navbar links
public function fetchAll()
{
$view = new View('links');
$view->links = $this->_linkModel->fetchAll();
return $view->display();
}
}// End LinksController class
(NewsController.php)
<?php
class NewsController
{
private $_newsModel = NUll;
public function __construct()
{
// create instance of NewsModel class
$this->_newsModel = NewsModel::factory('news');
}
// fetch all news
public function fetchAll()
{
$view = new View('news');
$view->heading = 'Breaking News';
$view->news = $this->_newsModel->fetchAll();
return $view->display();
}
}// End NewsController class
(PageController.php)
<?php
class PagesController
{
// constructor
public function __construct(){}
// display master web page using the HMVC design pattern
public function index()
{
$view = new View('layout');
$view->title = 'Using the HMVC design pattern in PHP 5';
$view->heading = 'Welcome to our web site';
echo $view->display();
}
}//End PagesController class
And last, but not least, below are the definitions of the template views that separately build different sections of a web page. Take a deep breath and look at them:
We're done. With this last code fragment I'm finishing this tutorial on using the HMVC design pattern in PHP 5. As usual, feel free to tweak all of the code samples shown in this article. The practice will arm you with a better understanding of the logic that drives this handy architectural pattern.
Final thoughts
It's hard to believe, but we've come to the end of the series. I hope that you've enjoyed reading it as much as I did writing it. Aside from the fun side, the primary goal of the series was to teach you in a friendly fashion how to implement the Hierarchical Model-View-Control design pattern in a truly useful way, based on the structure provided by an object-based framework.
As you may have guessed, the application of this pattern when developing PHP applications isn't mandatory, as a regular MVC implementation will give you most of the flexibility and scalability needed by small and mid-size projects. However, if you're a purist when it comes to building highly-modular applications, the HMVC paradigm will probably be the approach that will best suit your needs.
See you in the next PHP development tutorial!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.