How to use Zend Framework in CodeIgniter 2.0


Combine Zend Framework with Codeigniter will give great result and cutting development time. Intergrating Zend Framework into CodeIgniter 2.0 is very easy. First, you download new Zend Framework here . After downloading, unpack it package and get Zend folder. Put this Zend folder into app/libraries in CodeIgniter.

Then, we create Zend libraries :

in app/libraries/ we create Zend.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php if (!defined(‘BASEPATH’)) {exit(‘No direct script access allowed’);}

/**
 * Zend Framework Loader
 *
 * Put the ‘Zend’ folder (unpacked from the Zend Framework package, under ‘Library’)
 * in CI installation’s ‘application/libraries’ folder
 * You can put it elsewhere but remember to alter the script accordingly
 *
 * Usage:
 *   1) $this->load->library(‘zend’, ‘Zend/Package/Name’);
 *   or
 *   2) $this->load->library(‘zend’);
 *      then $this->zend->load(‘Zend/Package/Name’);
 *
 * * the second usage is useful for autoloading the Zend Framework library
 * * Zend/Package/Name does not need the ‘.php’ at the end
 */
class CI_Zend
{
    /**
     * Constructor
     *
     * @param   string $class class name
     */
    function __construct($class = NULL)
    {
        // include path for Zend Framework
        // alter it accordingly if you have put the ‘Zend’ folder elsewhere
        ini_set(‘include_path’,
        ini_get(‘include_path’) . PATH_SEPARATOR . APPPATH . ‘libraries’);

        if ($class){
            require_once (string) $class . EXT;
            log_message(‘debug’, "Zend Class $class Loaded");
        }
        else
        {
            log_message(‘debug’, "Zend Class Initialized");
        }
    }

    /**
     * Zend Class Loader
     *
     * @param   string $class class name
     */
    function load($class)
    {
        require_once (string) $class . EXT;
        log_message(‘debug’, "Zend Class $class Loaded");
    }
}

To use this libraries, for example i load Zend Json components :

1
2
3
4
5
$CI =& get_instance();
$CI->load->library(‘zend’);
$CI->zend->load(‘Zend/Json’);

$this->ZendJson = new Zend_Json();

It’s pretty easy right? 🙂

Kudos to http://www.beyondcoding.com/2008/02/21/using-zend-framework-with-codeigniter/


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.