Art AboutFAQWrite for usContact InfoRSS Feed
Search A/R/T:

Using Zend framework Components In CakePHP

by Daniel Hofstetter — Page 1 of 3
Click to rate:    
78 votes / avg. rating 16.74%

In this article I describe how to use Zend framework components in a CakePHP application by means of building a very simple CakePHP application using Zend_Service_Flickr, a component for accessing the Flickr web services.


 

Before we can start baking we have to do some preparations. First of all, this article is for those who are at least familiar with using CakePHP at a very basic level. If you are not yet introduced to CakePHP, please check out the Overview on CakePHP by Fabio Cevasco here at A/R/T. Now, onto the actual preparations:

1. Make sure you installed PHP 5 as the Zend framework requires PHP 5.

2. I assume you have already installed CakePHP. If that is not the case, download it from http://cakeforge.org/projects/cakephp/ and follow the installation instructions on http://manual.cakephp.org/chapter/3. I used version 1.1.6.3264 of CakePHP for this article.

3. Download the Zend framework from http://framework.zend.com/download.

4. Copy the content of the directory ZendFramework-0.1.5/library from the zipped file to <where_you_installed_cakephp>/app/vendors.

With that, the preparations are done. We are ready for baking our cake.

Baking, first iteration

In our first iteration we will build the skeleton of our application. Our goal is to get an empty page when calling our controller. Yes, I know, the result of an iteration should be something of value for the user, so this iteration is an exception.

We start with the model which we call simply “Flickr”. What functionality should our model provide? For our simple application we only need one method which returns images for a tag. So we get the following class:

  1. // app/models/flickr.php
  2. class Flickr extends AppModel
  3. {
  4.   var $useTable = false;
  5.    
  6.   function getImagesByTag($tag)
  7.   {   
  8.     return array();
  9.   }
  10. }
Notice that we have to add “var $useTable = false;” as our model will use a webservice and not a table.

Our model will be used, of course, by a controller. To make use of CakePHP's magic we name it “FlickrController”. The functionality of our controller is simple: it needs a method which gets the data from the Flickr model and hands the data over to the view. So we can write our controller:

  1. // app/controllers/flickr_controller.php
  2. class FlickrController extends AppController
  3. {
  4.   function show($tag = null)
  5.   {
  6.     $this->set('images', $this->Flickr->getImagesByTag($tag));
  7.   }
  8. }
The last step is the view. It is self-explanatory.

  1. // app/views/flickr/show.thtml
  2. foreach ($images as $image)
  3. {
  4. }
Our first iteration is almost done. Time to test what we have done. Open <yourdomain>/flickr/show/tree in your browser and you should get an empty page. Not really interesting, is it? So we move directly to the second iteration.

 


 Tags:  ,
 Add tag(s) (comma separated):
[Tags beta] — [Add New][Help]  

Tags Help

Tags are keywords associated with a web page that help classifying information. You can find a good explanation here.

To add one or more tags to this page, simply enter them below (separate them with a comma) and hit enter or click on the "Go" button.

New Comment
This form allows you to type in a new comment. Keep in mind the following:
  • The system accepts input in plain text format. Newlines will be converted to the HTML equivalent, and the system will try to catch most URLs and make them clickable.
  • Your e-mail address will never be displayed. We will use it only to notify you when new comments are posted to this page.
  • As a rule, we do not delete comments unless they are offensive, racist, spam or otherwise inappropriate.
  • Bold fields are required
Your Name:
Your e-mail:
Type this number:
Subject:
Comment:
Comments   New Comment
Re: Using Zend framework Components In CakePHP (#578)
By Patrick on 2007-08-06 09:38:24

Thanks for this tutorial but one question still remains:

When should I use a Zend component in a controller and when in a model?

Thanks in advance

[Reply to this]

Index