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).

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.


{mospagebreak title=Try It Out}

With the actions and views defined, its time for the moment of truth. Try navigating to the following pages and see what happens:

  1. To access the home page, navigate to this URL: http://localhost/
     
  2. To access the about.phtml view, navigate to this URL: http://localhost/about/
     
  3. To access the you.phtml view, navigate to this URL: http://localhost/about/you/ .

Next, consider experimenting by adding a new action and class and set of corresponding views. Just copy and rename one of the controllers, being sure to follow the same conventions used in the original class.

Searching the Web with Zend_Service_Yahoo

Table 25-1 presented just some of the dozens of Zend Framework components at your disposal, therefore as you might imagine it’s difficult to decide which to demonstrate in this brief chapter. After some consideration it seems ideal to introduce the Zend_Service_Yahoo component, as it shows how the framework can really shine at simplifying otherwise fairly complex operations, in this case Web Services interaction.

The Zend_Service_Yahoo component allows you plug into Yahoo!’s search engine, as well as search images, businesses, and news. Therefore, suppose you want to add a page to the chess club Web site that displays the latest chess news. This news page will appear at http://www.example.com/news/, meaning a new controller and view will need to be added.


Note In order to follow along with these examples youll need to register for a free Yahoo! application ID. Navigate to http://developer.yahoo.com/ for more information.

 


 

Create the Controller

The controller, named NewsController.php, should be placed in the application/modules/default/ controllers directory. This controller is responsible for retrieving the news via the Yahoo! component and sending that data to the view. The NewsController.php script is found in Listing 25-7.

Listing 25-7. The Chess Club’s News Controller (NewsController.php)

<?php 

    // Load the Zend_Controller_Action class
   
require_once(‘Zend/Controller/Action.php’);

    // Load the Yahoo! Service class
   
require_once(‘Zend/Service/Yahoo.php’);

    class NewsController extends Zend_Controller_Action
   
{

        public function indexAction()
       
{

            // Invoke the Yahoo! service
            $yahoo = new Zend_Service_Yahoo("INSERT_YAHOO_ID_HERE");

            // Execute the search
            $results = $yahoo->newsSearch("chess");

            // Send the search results to the view
           
$view->results = $results;

        }
    }

Of course, in a real-world situation you might use the controller to retrieve some user preferences from a database pertinent to region, allowing for more geographically targeted chess-related news results. Those preferences could then be passed to the view much in the same way the other properties were passed in previous examples.

Create the View

The view’s role is simple: render the search results in an easily readable format. This is done by looping through each result and outputting it to the browser. This file, named index.phtml, should be placed in the directory application/modules/default/views/scripts/news/. Listing 25-8 presents this simple but effective view.

Listing 25-8. The Chess Club’s News View (index.phtml)

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

<h4>The Latest Chess News</h4>

<?php
   
foreach ($this->results as $result) {
        printf("<p><a href=’ %s’>%s</a> | %s  <br />",
            $this->escape($result->NewsSourceUrl),
            $this->escape($result->NewsSource),
            date("F j, Y", $this->escape($result->PublishDate))
        );
        printf("%s </p> ", $this->escape($result->Summary));
}

?>

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

Executing this code will produce news-related output similar to that shown in Figure 25-1.


Figure 25-1.  Output of the latest chess news

Summary

This chapter provided but a glimpse of what the Zend Framework is capable of; but hopefully it has served its purpose: to get your mind racing about just how productive Web frameworks can make you.

In the following chapter, an introduction to Oracle, you’ll begin the next learning phase of this book. 

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort