** UPDATED on 2010-09-06 **

After several additional months of developing PHP applications with CodeIgniter, I am still very pleased with the platform and the suite tools that it provides. I know what it takes to write an application framework, so I appreciate the simplicity in its design. In my opinion, it is just enough framework to get the job done without restricting your development style, and that’s hard to come by.

That being said, I also like the suite of libraries that are part of the Zend Framework, especially the services! As of this post, they support Amazon, Delicious, Flickr, Google, Technorati, Twitter, Yahoo, and more! Now I know there is nothing like writing your own API integrations, and believe me, I’ve done my share; however there is something to be said for having someone else support those API integrations and keep them updated for you. You won’t hear any complaints from me!

You may think that integrating two different PHP frameworks is not possible, and that is probably true with other frameworks. However in this case, the Zend components are stand-alone and can be used-at-will, so this means they can easily be integrated into any CodeIgniter installation with very little effort. Nice!

As with any integration, there is more than one way to get the job done. So, I’ll just show you one way.

Step 1: Download the latest version of Zend Framework.

Extract the files and locate the “/library/Zend” directory. (This is where all of the components live).

Step 2: Copy the Zend directory to your CodeIgniter’s library directory.

At this point, you can either selectively copy the sub-directories or copy them all. Remember that the Zend components are stand-alone, so it’s up to you.

Step 3: Set the PHP include_path to the location of your Zend directory from Step 2.

Again, there are several ways to do this, but in my opinion, the simplest is to add an ini_set methods just below the BASEPATH define in your CodeIgniter’s index.php file or the applications dispatch file if you renamed it.

// defined for easy access to Zend libs by gotphp.com
if (!defined("PATH_SEPARATOR")) { 
	if (strpos($_ENV["OS"], "Win") !== FALSE) { 
		define("PATH_SEPARATOR", ";");
	} else { 
		define("PATH_SEPARATOR", ":"); 
	}
}
ini_set('include_path',ini_get('include_path').PATH_SEPARATOR.BASEPATH.'../system/application/libraries/');

Note: The above code should work for most CodeIgniter installations however your include path may vary. Simply update it as needed.

Step 4: Start using the integrated components by including them “at-will” and enjoy!

Here is a simple example of how to use Zend’s RSS library to read an RSS feed.

Example Controller:

class Zendexample extends MY_Controller {

	// --------------------------------------------------------------------
	
	/**
	 *	The constructor
	 */
	function __construct()
	{
		parent::__construct();	

		// IMPORTANT: You must require or include the Zend library you are 
		// about to use that has been uploaded to: /apps/main/libraries/Zend/*
		//
		// TIP #1: You only need to upload the libraries you plan to use. Some
		// Zend libraries have dependencies so those will need to be uploaded
		// too.
		// 
		// TIP #2: You can include the Zend files from anywhere in your app.
		require("Zend/Feed/Rss.php");
	}
	
	// --------------------------------------------------------------------
	
	/**
	 *	The default method for this controller
	 */
	function index()
	{

		// You can reference the Zend manual for how to use Zend libraries
		// http://framework.zend.com/manual/en/zend.feed.consuming-rss.html

		// Fetch an RSS feed using the Zend RSS library
		$output = new Zend_Feed_Rss('http://www.gotphp.com/feed');
		
		// Add the RSS to CodeIgniter's template data
		$this->tpl_data += array(
			'rss' => $output
		);
		
		// Call the display template (/apps/main/views/zendexample.php)
		//
		// TIP #1: $this->_tpl is defined in the parent controller MY_Controller
		// and wraps the template between header & footer templates already.
		//
		// TIP #2: $this->tpl_data will be available to all view templates so
		// you can add anything you want to it from this controller.
		$this->_tpl('zendexample');
	}
	
	// --------------------------------------------------------------------
	
}

Example View:

Zend Example - Pulling in an RSS Feed

RSS Feed: < ?=$rss->title();?>
    < ?php foreach($rss as $item): ?>
  • < ?=anchor($item->link(), $item->title(), 'target="_blank"')?>
  • < ?php endforeach; ?>

And here’s what the output will look like:

Zend Libraries Feed Example

I know this is a simple example, but there’s really not much to integrating Zend with CodeIgniter. For a lot of Zend’s libraries, it is that easy!

If you found this article useful, please share your comments below.

7 Responses to CodeIgniter with Zend Framework Libraries

  1. [...] CodeIgniter with Zend Framework Libraries – Another Zend and CodeIgniter blend tutorial. [...]

  2. [...] original post here: CodeIgniter with Zend Framework Libraries | Practical CodeIgniter … No [...]

  3. Vel says:

    Excellent article. I was in fact thinking to write a hook when i found this. Thanks a ton for saving my time.

  4. david says:

    Great article, very useful….

  5. Abhishek says:

    Great article. We recently started doing this…

  6. [...] Source: GotPHP GD Star Ratingloading… Today, 5:33 am No Comments Short URL [...]

  7. Jayesh Ambali says:

    Really nice article, this saved a lot of time for me as I was trying to use Zend library in my codeigniter project

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...