Welcome Guest, Not a member yet? Register   Sign In
Using Zend Framework components in CI
#1

[eluser]JoostV[/eluser]
Hi all,

Fred Wu posted a good tutorial on using Zend Framework components in your CI app.

He created a library that you only need to load if you're going to use it. This means no overhead until you actually call the class (unlike the hook used by Daniel Vecchiato -who is greatly admired nonetheless- that sets your include_path whether you use ZF or not)

Zend Framework has component with great functionality, such as
- API classes for use with numerous web services such as Flickr, GData, Twitter, etc
- A great Caching class, that also does partial caching and function caching
- Session and Auth classes
- Feed parser class
- Etc.

A lot of these classes can be used stand-alone.

After you've copied ZF and created the Zend library class, here's an example controller zendtest.php that uses Zend_Service_Flickr and Zend_Cache

Code:
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}

class Test extends Controller {

    function Test() {

        parent::Controller();

    }

    /**
     * Use of Zend_Service_Flickr and Zend_Cache and in Codeigniter.
     *
     * You need to get a Flickr API key before you can use the
     * Zend_Service_Flickr class!     *
     * Get it at http://www.flickr.com/services/api/keys/apply/
     */
    function zendcache()
    {

        // Load libraries
        // Don't forget to copy ZF and the Zend.php library first!
        $this->load->library('zend');
        $this->zend->load('Zend/Cache');
        $this->zend->load('Zend/Service/Flickr');

        // Set up Zend_Cache
        // In this example, we use a 600 second lifetime for cache, so 10 minutes
        $cache = Zend_Cache::factory(
            'Core',
            'File',
            array('lifetime' => 600, 'automatic_serialization' => true),
            array('cache_dir' => BASEPATH . 'cache')
        );
        
        ## REPLACE null WITH YOUR FLICKR API KEY! ##
        // Set the Flickr API key
        $flickr_api_key = null;

        // Create the output if the cache missed
        if (!$output = $cache->load(md5($this->uri->uri_string()))) {

            // Set up the Zend_Service_Flickr class
            $flickr = new Zend_Service_Flickr($flickr_api_key);

            // Set the search tag that we are going to search Flickr for
            $tag = 'pindakaas'; // deafult search
            if ($this->uri->segment(3)) {
                $tag = $this->uri->segment(3);
            }

            // Search Flickr for this tag
            $results = $flickr->tagSearch($tag);

            // Create feedback
            $output = '<h1>Add a tag in segment 3 of the URI</h1>';
            $output .= '<p>You searched Flickr for <em>' . $tag . '</em> (' . date('d-m-Y H:i:s') . ')</p>';
            $output .= '<p>' . $results->totalResultsAvailable . ' result(s) found</p>';

            // Show images if there are any
            if ($results->totalResultsAvailable > 0) {

                foreach ($results as $result)
                {
                    $output .= '<img >Small->uri . '" alt="' . $result->title . '" />' . ' ';
                }
            }

            // Cache key is an md5 hash of the complete URI, including ALL parameters
            $cache->save($output, md5($this->uri->uri_string()));
        }

        // Echo to screen (we should really do this in a view file, but hey...)
        echo $output;

    }

}


Messages In This Thread
Using Zend Framework components in CI - by El Forum - 12-12-2008, 04:01 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 04:10 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 06:28 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 06:37 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 06:46 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 07:57 AM
Using Zend Framework components in CI - by El Forum - 12-12-2008, 08:14 AM
Using Zend Framework components in CI - by El Forum - 12-13-2008, 11:17 PM
Using Zend Framework components in CI - by El Forum - 12-14-2008, 04:10 PM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 02:59 AM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 02:59 AM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 04:31 AM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 06:28 AM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 08:14 AM
Using Zend Framework components in CI - by El Forum - 12-15-2008, 09:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB