Home arrow PHP arrow Using the MVC Paradigm with the Zend Framework

Using the MVC Paradigm with the Zend Framework

In this conclusion to a four-part series on MVC and the Zend framework, we'll finish the application we started in a previous part, try it out, and demonstrate another use for the framework. This article is excerpted from chapter 25 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Using the MVC Paradigm with the Zend Framework
  2. Try It Out
By: Apress Publishing
Rating: starstarstarstarstar / 1
September 21, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Defining the Views

Next we'll create the views that correspond to these three actions: one for the home page, one for the /about/ page, and one for the /about/you/ page. The home page view should be placed in the directory /application/modules/default/views/ scripts/index/, and the other two in /application/modules/default/views/ scripts/about/. These views are presented in Listings 25-4, 25-5, and 25-6, respectively. Each of these views is intended to demonstrate different facets of the behavior of views.

Listing 25-4. The index.phtml View

<?php
   
echo $this->render('header.phtml');
?>

<div id="header">Next Chess Club Meeting: April 12</div>

<p>

Welcome to our Chess Club's Web site! We're a bunch of chess enthusiasts
who travel the globe in search of worthy opponents. Join us at our next
meeting, held at the coffee shop on the corner of Third and Neil
each Tuesday at 6 p.m.
</p>

<?php
   
echo $this->render('footer.phtml');
?>

Listing 25-5. The index.phtml View

<?php
   
echo $this->render('header.phtml');
?>

<div id="header">About Our Chess Club</div>

<p>
    Founded: 1997<br />
    City: Columbus, Ohio<br />
    Where we meet: Cup of Love, corner of Third and Neil<br />
    When we meet: Each Tuesday at 6 p.m.<br />
    Notes: Bring your board and pieces if you have them!
</p>

<?php
   
echo $this->render('footer.phtml');
?>

Listing 25-6. The you.phtml View

<?php
   
echo $this->render('header.phtml');
?>

<div id="header">About You!</div>

<p>
    Your IP Address: <?php echo $this->escape($this->ip); ?><br />
    Your Browser: <?php echo $this->escape($this->browser); ?><br />
</p>

<?php
    echo $this->render('footer.phtml');
?>
   

As demonstrated in these views, you should pass all data originating in the controller through the escape() method, as it will properly filter data through PHP's htmlspecialchars() function.

You'll see each of these views refer to header.phtml and footer.phtml files (both of which are available at the book's Source Code/Download page at http://www.apress.com), which serve as the page template headers and footers, respectively. These global templates can be placed in the /application/modules/default/views/scripts/ directory and will automatically be located and integrated into the view when using the render() method. Not surprisingly, the header could include references to the page masthead as well as the CSS and JavaScript files. The footer could include things such as copyright information and the closing page tags.


Tip  Quite conveniently, the Zend Framework supports the ability to take advantage of more sophisticated templating solutions than those demonstrated here, such as Smarty (see Chapter 19). See the Zend Framework manual for more information.




 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: