Web Frameworks - Workbook - Week 07

From mi-linux
Revision as of 12:13, 2 February 2010 by In9352 (talk | contribs)
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 martching 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);
       
      /* Pass results 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);
       
      /* Pass results 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