Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - Creating a Web Page Controller with th...
MISCELLANEOUS CODE

Creating a Web Page Controller with the HMVC Design Pattern
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2010-04-14

    Table of Contents:
  • Creating a Web Page Controller with the HMVC Design Pattern
  • Review: building different sections of a web page using the MVC pattern
  • Building a page controller and a master view
  • The sample application's full source code

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    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:

    (links.php)

     

     

    <div id="navbar">

    <?php if (empty($links)):?>

        <h2>You cannot navigate this site!</h2>

    <?php else:?>

        <ul>

        <?php foreach($links as $link):?>

            <li><a href="<?php echo $link->href;?>"><?php echo $link->text;?></a></li>

        <?php endforeach?>

        </ul>

    <?php endif?>

    </div>

    (news.php)

     

     

    <div id="newscontainer">

    <?php if (empty($news)):?>

        <h2>There are not news in the database.</h2>

    <?php else:?>

        <h2><?php echo $heading;?>

        <?php foreach($news as $new):?>

            <h3><?php echo $new->title;?></h3>

            <p><?php echo $new->text;?></p>

            <hr />

        <?php endforeach?>

    <?php endif?>

    </div>

    (layout.php)

     

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

        <head>

            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

            <title><?php echo $title;?></title>

        </head>

        <body>

            <h1><?php echo $heading;?></h1>

            <!-- Render navigation bar -->

            <?php echo Request::load('LinksController', 'fetchAll');?>

            <!-- Render news section -->

            <?php echo Request::load('NewsController', 'fetchAll');?>

        </body>

    </html>

     

    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.
    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap