Coupling Zend Framework with CodeIgniter

CodeIgniter is a super cool web application framework for PHP. It is lightweight and lets you get the jobs done in a fashion. It is the first framework I have used and I am still using it. On the other hand, Zend Framework is from Zend, the PHP company. I appreciate the huge library and the full stack nature. I respect the fact that ZF is from the people who maintain PHP. But honestly, I don’t find it feasible for medium and small projects.

One of the brighter aspects of the Zend Framework is that it has many cool libraries. The framework itself is loosely tied and the components are available as stand alone. We can take this opportunity to integrate the powerful library of the Zend Framework into the beautiful CodeIgniter making ourselves more productive than before. Specially, we can relieve ourselves from writing and maintaining wrappers to different APIs.

Ok then, let’s see how do we integrate the Zend Framework libraries into CI.

1) Setup CodeIgniter.

2) Download the Zend Framework. Extract the archive.

3) From the ZF files, copy the “Zend” directory from inside the “library” directory.

4) Paste the directory into the “system/application/libraries” directory. So ultimately, the new location of the copied “Zend” directory would be “system/application/libraries/Zend”. If you’re on Linux/Unix, we need to deal with file permission. Make the Zend directory accessible by all. I don’t think I need to tell you how to do that with chmod, do I ? ;)

5) In the same “system/application/libraries/” directory, create a new file named “Zend.php” and put the following contents:

 
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
 
class Zend
{
 
	function __construct()
	{
 
		ini_set('include_path',
		ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
 
	}
 
 
	function load($class)
	{
		require_once (string) $class . EXT;
 
	}
}
 
?>

:) We are done. That completes the integration. Now we can load any Zend component inside our controllers. Let’s make our hands dirty by modifying the default welcome controller. Open the “system/application/controllers/welcome.php” and put the following contents:

<?php
 
class Welcome extends Controller {
 
	function Welcome()
	{
		parent::Controller();
	}
 
	function index()
	{
		 $this->load->library('zend');
		 $this->zend->load('Zend/Service/Flickr');
 
		$flickr = new Zend_Service_Flickr('12e99caebb8f305fff5a943606ecde18');
 
		$results = $flickr->tagSearch('worldcup');
 
 
 
		foreach ($results as $result)
		{
 
                      $photo = $result->Small;
                      echo "<a href=\"{$photo->clickUri}\"><img src=\"{$photo->uri}\" /></a><br /><br />";
 
		}
 
	}
}
?>

If you read the codes above, you already know what it does :) If everything goes right, you’ll see a bunch of Flickr photos with the “worldcup” tag. Clicking on a photo will take you to the Flickr page of that photo.

Now, what we have seen is pretty straightforward. We don’t use any hooks or don’t go for any complexity. We simply put the Zend library in the application library. Then we write a loader class that loads Zend libraries. When we construct this “Zend Loader” object, it adds the Zend library directory to the include path. Then when we use the load() method of the object, it imports the library and adds the functionalities to the current scope :)

Enjoy the Zend Services with the cool CodeIgniter framework! Have fun :D

This entry was posted in Blog Post and tagged , . Bookmark the permalink.

13 Responses to Coupling Zend Framework with CodeIgniter

  1. Pingback: Coupling Zend Framework with CodeIgniter « maSnun's logs | Programming Blog Imagik.org

  2. Emran Hasan says:

    Good post! Very clearly described :)

  3. Lenin says:

    Very impressive! Carry on your good works :)

  4. Nuhil says:

    I think this is called a Cool post. :)

  5. Pingback: Tweets that mention Coupling Zend Framework with CodeIgniter » maSnun's logs -- Topsy.com

  6. Hasin says:

    Hi Masnun

    Excellent post I must say. I was also wondering how to use the service libraries from ZF in CI :) and you shed some light :)

    Carry on good posting :)

  7. Simply and awesome! tks buddy!!

    With these examples we can go on and implement a lot more of libraries!

    Keep Posting! ;)

  8. masnun says:

    If you were not a stupid joker, you would have seen that I didn’t use hooks like that tutorial.

    Thanks for the entertainment :)

  9. Tareq says:

    I think google bot wanted to say about this. Lolz

  10. masnun says:

    That one is pretty similar except that I implemented the Zend loader in a bit different way :)

  11. um9i says:

    Hi !
    Greate Post !
    But with ZF v.1.10.x & CI v.1.7.2 , i have an Error :

    “An Error Was Encountered
    Unable to load the requested class: zend”

    Can’u help, please ?

  12. um9i says:

    Ou, sorry, i find an error in my syntax .
    Now, all works perfectly.

    Thanks =)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">