Web Frameworks - Workbook - Week 07

From mi-linux
Jump to navigationJump to search

Main Page >> Web Frameworks >> Workbook >> Workshop - week 07

Zend provides many classes to access a variety of APIs via its collection of Zend_Service classes.

Today let's have a look at the Zend_Service_Simpy class.

Zend_Service_Simpy

Simpy is a social bookmarking service. With Simpy, you can save, tag and search your own bookmarks and notes or browse and search other users' links and tags. You can be open and share your links with others, or keep them private.

Simpy also helps you find like-minded people, discover new and interesting sites, publish your bookmarks, detect and eliminate link-rot, etc.

So before you start, create an account, it's free!

Now, let's see how you can display your favourite websites on your Zend application.

Create a new controller and view

First, run the following line of command:

zf.sh create controller simpy

A new "simpy" controller and matching view will be created.

The controller

This is the code we need in our new controller. We simply create a "Zend_Service_Simpy" object and use it to connect to our Simpy account.

We then retrieve the last 20 links added, and the 5 last links added with a certain tag ("Radiohead" in my case!)

Finally we pass the 2 sets of the data to the view.

<?php

// application/controllers/SimpyController.php

class SimpyController extends Zend_Controller_Action
{
    public function indexAction()
    {
      // Here we set up the account username and password. 
      // Go to www.simpy.com and sign up for an account, it's free!
      $user = "YOUR OWN SIMPY USER NAME";
      $pass = "YOUR OWN SIMPY PASSWORD"; 
       
      // Create a new instance of the simpy object, using our username and password.
      $simpy = new Zend_Service_Simpy($user, $pass);
       
      /* Search for the 20 links added most recently */
      $linkQuery = new Zend_Service_Simpy_LinkQuery();
      $linkQuery->setLimit(20);
       
      /* Get results and pass to view */
      $linkSet = $simpy->getLinks($linkQuery);
      $this->view->entries = $linkSet;
      
      /* Next, search for the 5 links added most recently tagged with 'Radiohead' */
      $linkQuery->setQueryString('tags:Radiohead');
      $linkQuery->setLimit(5);
       
      /* Get results and pass to view */
      $linkSet = $simpy->getLinks($linkQuery);
      $this->view->entries_tag = $linkSet;
      
    }
}

All we need now is to get our view to display the data.

The view

The view itself it quite simple. We loop through our 2 sets of links, and display them on the screen. (one is called "entries" and one "entries_tag", as specified in the controller above).

<h1>Alix's favorite websites</h1>

<dl>
    <?php foreach ($this->entries as $entry): ?>
    <dt>
      <a href="<?php echo $this->escape($entry->getUrl()) ?>">
      <?php echo $this->escape($entry->getTitle()) ?>
      </a>
    </dt>
    <?php endforeach ?>
</dl>

<h1>Alix's favorite Radiohead websites</h1>

<dl>
    <?php foreach ($this->entries_tag as $entry): ?>
    <dt>
      <a href="<?php echo $this->escape($entry->getUrl()) ?>">
      <?php echo $this->escape($entry->getTitle()) ?>
      </a>
    </dt>
    <?php endforeach ?>
</dl>

Bg-checkpoint.png Checkpoint

You should see something like this when visiting your new page:

Zend 06.gif