A look at how to utilize components of the Zend Framework within a CodeIgniter application with the aim of keeping things simple.
Whilst I am not a huge fan of the Zend Framework as a whole (it’s always felt more like a monolithic PEAR like library than a Framework to me), there are undoubtedly some very useful features and classes that are held within it.
This aim of this article is to show how we can use whichever aspect of the Zend Framework we want, from within a CodeIgniter application with a minimum of fuss. The goal is to be able to use Zend components, without littering our beautiful, lightweight CodeIgniter application with php requires and without coupling ourselves too strongly to the Zend Framework. The result of this is that should you want to drop the Zend Framework at a later date, you won’t have to pull your code to bits to replace it.
I hear you all. There have been many libraries written for CodeIgniter that do some of the things the Zend Framework does. Elliot Haughin has done a great job in writing many libraries for various applications including, Twitter, Facebook Connect and Last FM (full list available at Elliot Haughin’s Site.
However, whilst these are great (and I highly reccomend them for sites where you’re only going to need a couple), if you are building a larger, complex application, then many of the Zend libraries that there is not yet a CodeIgniter implementation of, can be very very useful and save you bags of time in writing your own libraries. (Zend Cache, Zend AMF, Zend ACL as a couple of examples).
The Zend Framework is also very well maintained and updated on a regular basis, so if you’re yearning for a feature, chances are other people will be and it will show up in a not too distant release.
Fortunately, implementing the Zend Framework in this way is a fairly simple excercise.
If you haven’t got it, you’re going to need the Zend Framework. You can get it from the Zend Framework Website.
Once you’ve got it, create the following directory in your CodeIgniter installation
../system/contrib/Zend
Extract the downloaded Zend Framework and move the files from the “Zend/library” directory it into the “Zend” directory we just created.
You should now have a directory structure like the following:
../system/
contrib/
Zend/
Acl/
Acl.php
Amf/
Amf.php
...
If you’re struggling with this, download my contrib folder and extract it to your codeigniter “system” directory.
Next we need to create a helper to do the loading for us. Create a new php file called “zend_framework_helper” in your helpers directory and place the following code in it.
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/");
require_once 'Zend/Loader.php';
We could now call the Zend Loader anywhere by using:
$this->load->helper(‘zend_framework”);
However, it’s probably going to be easier if we use CodeIgniter’s autoloader. Place “zend_framework” in the helpers section of your application/config/autoload.php file. It should look something like this:
$autoload['helper'] = array('url','text','zend_framework');
We’re now ready to start using the Zend Framework components in our CodeIgniter application, but before we go any further, it’s worth running your application to make sure you’re not receiving any errors.
Once you’re happy that your application is running OK, you can simply initialize the Zend component you want to use by calling the Zend_Loader::loadClass method in your code and passing it the name of the component you wish to use:
Zend_Loader::loadClass(‘Zend_Cache’);
I will be covering an actual use of one of the Zend Components (Zend_Cache) in a future post.
So, there you have it, the Zend Framework implemented in CodeIgniter.
I hope have found this useful, please let me know via comments of any issues or feedback you may have.
Note: it is entirely possible to only move the Zend components you are interested in, rather than the entire framework, into your CodeIgniter application, but you will have to check it’s dependencies in the Zend Framework Documentation. Most of the Zend libraries seem to depend on "Exception.php" but I have not extensively researched this. I would recommend you always include the “Loader/” directory and “Loader.php” as this makes loading the Zend Components very easy and avoids littering your code with lots of "include" and "require" statements.
If this article interested you, you may find the following books (from amazon) of use.
Hi, thanks for your comment, I’m glad you found it useful. I’ll have a look at getting it working without the now deprecated Zend_Loader. I’ll then update the post.
I’ve just implemented the latest version (1.8.3) on this site and the implementation described here does still work correctly.
Having read through the Zend documentation, it would seem that Zend_Loader itself is not deprecated, but the registerAutoload method has been moved into its own class (Zend_Loader_Autoloader).
Consequently, my tutorial above should still be applicable. Let me know if you do run into problems using it.
It might actually be me worth describing how to do the same thing but using autoload instead of loadClass. I will update this post with details.
Thanks for pointing out the possible issue though, I hadn't noticed the changes to Zend_Loader in 1.8
Great post Phil!
Using Codeigniter and Zend together is the perfect solution for me. I love using codeigniter for the speed, both in terms development and the application itself. To have the power of the Zend libraries at your fingertips too is a match made in heaven.
This guide is a perfect ‘how to’ and I shall bookmark for future reference, Thanks!
Hello Phil,
I just followed your tutorial for implementing the zend libraries into CodeIgniter, everything seems to work fine. But when i try to load the Acl component with Zend_Loader::loadClass(‘Zend_Acl’); it gives me the following error message:
Fatal error: Class ‘Zend_Loader’ not found in C:\xampp\htdocs\\Lib\Frameworks\CodeIgniter\system\application\controllers\ACL\acl.php on line 2
any idea what i’m doing wrong?
Hi Emile,
Looks like you might have set up the location of your Zend directories wrong.
Make sure you put your Zend Framework folder in Codeigniter/system/application/contrib
Then your zend_framework_helper.php path must be as follows:
ini_set(“include_path”, ini_get(“include_path”).PATH_SEPARATOR.BASEPATH.”/contrib/”);
Unless you have changed your basepath in which case you need replace BASEPATH with the path to your CodeIgniter system folder.
Hope that makes sense.
dddd
Phil is a Senior Web Developer, Programmer, Technical Writer and Fisherman based in London.
jeison
Jun 16th 2009
Hi, very helpfull your tutorial, thanks, just one question, what version of zend framework are you using,because Zend_loader is deprecated on 1.8, how to fix it you for the tutorial,
again thanks