How to implement Zend Framework with Codeigniter

Download Files

There are quite few posts how to use Zend framework in Codeigniter.

  1. Beyond Coding
  2. Freakauth
  3. Phil’s post

However none of them worked on my XAMPP. I thought there might be some who are struggling to use Zend frame work on Windows. Here is how I did.

Here you can read why they don’t work with Windows.

Adding Zend Framework

Here you can read how to add Zend Framework. And I use this structure for this post.

You need to create a folder ../system/contrib/ and add Zend folders in this folder.

../system/ contrib/ Zend/ Acl/ Acl.php Amf/ Amf.php ...

Create a file system/application/helpers/zend_framework_helper.php and add the following.

<?php
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.str_replace("/", "\\", BASEPATH)."contrib\\");
require_once ('Zend/Loader.php');
?>

If you are not using Windows, then add the following instead.

ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.BASEPATH."/contrib/");
require_once 'Zend/Loader.php';

Adding it in autoload.php

Open config/autoload.php and add the following.

$autoload['helper'] = array('zend_framework');

Using Zend in Codeigniter

Now you can load any Zend class by adding Zend_Loader::loadClass(‘ZEND-CLASS-HERE’);.
You can see all the Zend classes here. http://framework.zend.com/manual/en/

Here are three examples.

Zend_Service_Flickr

Create a file controllers/flickr.php and add the following.

<?php
class Flickr extends Controller
{
function __construct()
{
parent::Controller();

}

function index()
{

Zend_Loader::loadClass('Zend_Service_Flickr');

$flickr = new Zend_Service_Flickr('ADD-YOUR-KEY-HERE');
try
{

$results = $flickr->tagSearch("cat");
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>';
}

}
}

?>

Don’t forget to change ADD-YOUR-KEY-HERE to your Flickr key.
If you don’t have one, get it from here.
Then visit your controller, http://127.0.0.1/codeigniter/index.php/flickr to see the result.

Using Zend_Gdata_YouTube

Create another file controllers/youtube.php and add the following.

<?php
class Youtube extends Controller
{
function __construct()
{
parent::Controller();

}

function index()
{

    Zend_Loader::loadClass('Zend_Gdata_YouTube');
    $youtube = new Zend_Gdata_YouTube();
    $query = $youtube->newVideoQuery();
    $query->videoQuery = 'cat';
    $query->startIndex = 10;
    $query->maxResults = 20;
    $query->orderBy = 'viewCount';

    $data['queryurl'] =  $query->queryUrl;
    $data['videofeed'] =  $youtube->getVideoFeed($query);
    $this->load->view('youtube_view', $data);
  }
}

?>
views/youtube_view.php

Create this file and add the following.

<?php

foreach ($videofeed as $videoEntry) {
    echo "---------VIDEO----------<br />";
    echo "Title: " . $videoEntry->getVideoTitle() . "<br />";
    echo "\nDescription:<br />";
    echo $videoEntry->getVideoDescription();
    echo "<br /><br />";
}
?>

Then visit a new page http://127.0.0.1/codeigniter/index.php/youtube to the result.

Using Google calendar

Create controller/googlecalendar.php and add the following. Use your google email and password.

<?php
class Googlecalendar extends Controller
{
function __construct()
{
parent::Controller();

}

function index()
{
    Zend_Loader::loadClass('Zend_Gdata_Calendar');
     Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
    // Parameters for ClientAuth authentication

$user = "YOUR gmail HERE";
$pass = "YOUR PASS";

$service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$service = new Zend_Gdata_Calendar($client);

try {
    $listFeed= $service->getCalendarListFeed();
} catch (Zend_Gdata_App_Exception $e) {
    echo "Error: " . $e->getMessage();
}
echo "<h1>Calendar List Feed</h1>";
echo "<ul>";
foreach ($listFeed as $calendar) {
    echo "<li>" . $calendar->title .
         " (Event Feed: " . $calendar->id . ")</li>";
}
echo "</ul>";
}
}
?>

Then again visit http://127.0.0.1/codeigniter/googlecalendar.

Download Files

3 comments to How to implement Zend Framework with Codeigniter

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>