4webby web applications
06
Mar 2007

Using Zend Framework components in Code Igniter

Author: Daniel Vecchiato (danfreak)

This tutorial will introduce you to an easy way to integrate the Zend Framework libraries (a lot!) into Code Igniter php Framework.

In particular we will build an example in wich we use the Zend_Service_Flickr to retrieve images based on a specific tag ('climbing' in this tutorial).

We didn't test all Zend Framework libraries with Code Igniter, therefore we can't ensure that this integration will work with all of them.

RESOURCES:

REQUIREMENTS:

INSTALLATION:

  1. download Code Igniter 1.5.2 from the Code Igniter website and install it following the CI Userguide notes
  2. download Zend Framework from the Zend Framework website
  3. unzip Swift-X.0.X-phpX.zip in a local folder
  4. create a folder called my_classes in your system/application directory
  5. copy the files INSIDE the folder ZendFramework-0.8.0\library\ in your system/application/my_classes/ directory
  6. Your system/application/my_classes/ directory will look as follows

    my_classes/
    |-Zend.php
    |-Zend/
    |-Acl.php
    |-Auth.php
    |-etc.
    |-Acl/

    |-Auth/
    |-etc.

  7. enable hooks in your application/config/config.php file
  8. /*
    |--------------------------------------------------------------------------
    | Enable/Disable System Hooks
    |--------------------------------------------------------------------------
    |
    | If you would like to use the "hooks" feature you must enable it by
    | setting this variable to TRUE (boolean). See the user guide for details.
    |
    */
    $config['enable_hooks'] = TRUE;

  9. add the following lines in application/config/hooks.php
  10. $hook['pre_controller'][] = array(
    'class' => 'MyClasses',
    'function' => 'index',
    'filename' => 'MyClasses.php',
    'filepath' => 'hooks'
    );

  11. in your system/application/hooks/ create a new file called MyClasses.php with the following inside
  12. <?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');

    class MyClasses
    {
    /**
    * includes the directory application\my_classes\ in your includes directory
    *
    */
    function index()
    {
    //includes the directory application\my_classes\
    //for windows tests change the ':' before BASEPATH to ';'
    ini_set('include_path',ini_get('include_path').':'.BASEPATH.'application/my_classes/');
    }
    }

    ?>

    Be carefull to change the colon ':' to semicolon ';' if you use the above hook while running PHP and Apache on a Windows platform

    PHP/Apache on Windows platform:
    //Windows
    ini_set('include_path', ini_get('include_path').';'.BASEPATH.'application/my_classes/')


    PHP/Apache on Linux platform:
    //Linux
    ini_set('include_path', ini_get('include_path').':'.BASEPATH.'application/my_classes/')

  13. build a controller to test that everything works fine (example taken here)
  14. <?php
    class Flickr extends Controller
    {
    function Flickr()
    {
    parent
    ::Controller();

    }

    function index()
    {
    require_once('Zend/Service/Flickr.php');

    $flickr = new Zend_Service_Flickr('YOUR_FLICKR_API_KEY HERE');
    try
    {

    $results = $flickr->tagSearch("climbing");
    if ($results->totalResults() > 0)
    {

    foreach ($results as $result)
    {
    echo '<img src="'.$result->Square->uri.'" />';
    }

    }
    else
    {
    echo '<p style="color: orange; font-weight: bold">No Results Found.</p>';
    }
    }
    catch (Zend_Service_Exception $e)
    {
    echo '<p style="color: red; font-weight: bold">An error occured, please try again later. (' .$e->getMessage(). ')</p>';
    }

    }

    }

    ?>

    now point your browser to http://www.your_website.com/flickr and you should see a bit of colourfull images!

  15. now build other controllers/methods to suit your needs. Check out the Zend Framework MANUAL
  16. HAPPY CODING!