Miscellaneous Code

  Home arrow Miscellaneous Code arrow Page 4 - Coding Controllers and Views for the H...
MISCELLANEOUS CODE

Coding Controllers and Views for the HMVC Design Pattern
By: Alejandros Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2010-04-07

    Table of Contents:
  • Coding Controllers and Views for the HMVC Design Pattern
  • Review: building a simple HMVC-based program
  • Splitting up the rendering of web page sections with controller classes
  • Completing the MVC triads

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Coding Controllers and Views for the HMVC Design Pattern - Completing the MVC triads


    (Page 4 of 4 )

    In the preceding section, I defined the controller classes responsible for fetching links and news headlines from the corresponding models. Now, the next step that I’m going to take will consist of building the view files that render that data within different sections of an HTML page.

    First, here’s the template file that displays the link bar of the web page in question:

    (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>

    That was really easy to code, wasn’t it? Now that you can see how this first view file looks, I guess that you’ll have a clearer idea of how it fits into the schema of an MVC triad. As seen above, this template file will render only the navigation bar of the web page by using the data passed by the “LinksController” class, which at this time, uses its associated model to access the corresponding MySQL table.

    On the other hand, the following view will display the main section of the web page, which will contain some fictional news:  

    (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>

    The creation of this view file completes the other MVC triad required to build the main section of a web page. As this one and the previous view render partials of the page, obviously it’s mandatory to code a master layout that includes those partials.

    Building the master layout will be the conclusion of this series of articles. So, in the meantime feel free to tweak the code of the controllers and views defined before, which should give you a more solid understanding of how to implement the HMVC design pattern in PHP 5. 

    Final thoughts

    In this penultimate installment of the series, I added something to the sample web application being developed here -- namely, a couple of MVC modules that will be used for building the link bar and the main section of a web page. Undoubtedly, the question that comes up at this point is: how will these modules accomplish that task?

    Well, here’s where the HMVC design pattern comes in. In the final chapter I’m going to define a generic web page controller, which will use the previous MVC triads to render sections of the aforementioned web page. Best of all, this will be achieved with one single HTTP request.

    My suggestion is simple: don’t miss the last article!


    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 6 - Follow our Sitemap