Home arrow PHP arrow Page 2 - Using the MVC Paradigm with the Zend Framework

Try It Out - PHP

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

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. 



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

blog comments powered by Disqus
   

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

 



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

Dev Shed Tutorial Topics: