donderdag 20 maart 2008

HOWTO Use Zend Framework in CakePHP

This article will describe shortly how you can extend the power of CakePHP using the Zend Framework. I know, earlier this week I talked about war when writing about these two. But, reactions from Wil and Richard changed my mind a little bit. Richard mentioned about the possibility to use Zend Components within CakePHP. I tested this for myself and it took me only a few minutes.

The Zend Framework seems a little bit more flexible in a way that you choose for yourself about which components you use and which you don't use. CakePHP has a certain pattern in which you have to develop your applications. CakePHP is the framework I choose and like to work with. But, it would be handy if it could be easier to integrate with webservices or create PDFs etc. It is possible, but you have to look for the right component. The Zend Frameworks has many components built-in. Like support for Flickr and Delicious. I tried these two and that works ok.

Ok, start the engines...


  • Download and install CakePHP and create a controller to use, for example main_controller.php


  • Download the Zend Framework 1.5 and extract it. Put the folder library/Zend in app/vendors so you have app/vendors/Zend/Service/ etc...


  • Create a function index in your controller. Don't forget to fill in your Flickr API Key




function index() {
define('MY_API_KEY', 'yourkeyhere');

$path = APP.'/vendors';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
vendor('Zend/Service/Flickr');

$flickr = new Zend_Service_Flickr(MY_API_KEY);
$results = $flickr->tagSearch('zend');
$this->set('images', $results);
}




  • And put in your index.ctp view:




< ?php foreach ($images as $image): ?>
< ?php echo $html->image($image->Thumbnail->uri); ?>
< ?php echo "{$image->title}\n"; ?>
< ?php endforeach; ?>




  • Open /main/index in your browser



I tried this with Del.icio.us also and it works great. Let us know if you tried other components and what the results are!




Just modified code a little thanks to Richard.

Have a look at http://framework.zend.com/wiki/display/ZFPROP/Home. They are working on more stuff. For example a Twitter component. Works too, but you have to:

vendor('Zend/Rest/Client');
vendor('Zend/Service/Twitter');

6 opmerkingen:

  1. Someone listening to me? That would be first! ;-)

    I've been using the Zend Search Lucene component for general searching in a commercial application I'm writing at the moment.

    My implementation code is pretty much identical to yours except I didn't know about the PATH_SEPARATOR constant so I was calculating it for myself. Thanks for the tip :-)

    BeantwoordenVerwijderen
  2. oh, one minor tweak to your code: I used:

    $path = APP . "vendors";

    BeantwoordenVerwijderen
  3. @Richard

    thanks, changed my code.

    Yahoo also works...

    BeantwoordenVerwijderen
  4. Hi,

    I just tried using Zend's Flickr service with cake 1.2 and no offense but it's slow. I'm getting processing times between 4-6 seconds.

    I think the reason for crappy performance is that the Zend services are dependent on so many of it's own files. I had to include these directories; Http, Rest, Service, Uri and Validate and these files from Zend - Loader.php, Uri.php and Registry.php

    BeantwoordenVerwijderen
  5. @Manny,

    It could be, maybe they will improve this in the future. But, do you load all the files you mentioned? I placed the whole library in my vendors dir, but only used the vendor Flickr, maybe this file loads the other files, but I didn't mention them in my code.

    BeantwoordenVerwijderen
  6. I got a very good tutorial here

    http://www.techflirt.com/cakephp-zend-framework-fusion/

    BeantwoordenVerwijderen