To support simplicity, possiibility and cost effective of PHP

Using Zend components in your own web project

Jan 15th, 2008 | By admin | Category: PHP, Tutorials

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Loading ... Loading ...

I am a fan of Prado but Zend framework takes my love with its set of components. Having an MVC framework with a higher level of intefration between Model – View – Controller is better than taking Zend basic MVC model into a real life project. However, using Zend components to complete common web features is a dream.

Zend components are not only easy to use but also designed with standards and deep thought. They are also very isolated so that coders are not required to use Zend MVC framework to use a component. Off course, there are some dependencies you have to take care of, for example, Zend_Authentication depends on Zend_Session or Zend_Feed depends on Zend_Http.Zend library is about 13Mb in its latest release at this moment, contains the whole framework suite and examples.

  • To download Zend framework: http://framework.zend.com/download
  • To download API and Programmer ref. guide: http://framework.zend.com/download/documentation/

Here are steps to take Zend components in to use in my own project:

  1. Extract ZendFramework/library/Zend into my /includes folder. Actually, I put only components I need but if you don’t want to concern with component dependencies, put them all here.
  2. Zend will make a lot of include file once you include one component into your code. So in order to forget this including process, create an __autoload() function for PHP5 to take of this inclusion.
    // For loading classes
    function __autoload($class_name) {
    $filename = $class_name . '.php';
    include_once($filename);
    }
    
  3. Then you have to add /includes/Zend into your include_path as you will don’t know where does a Zend component want to include its dependency or at least, you really don’t care.
    $includePath = get_include_path();
    $includePath .= PATH_SEPARATOR . SitePath . 'includes';
    $includePath .= PATH_SEPARATOR . SitePath . 'includes' . DIRSEP . 'Zend';
    $includePath .= PATH_SEPARATOR . SitePath . 'framework';
    $includePath .= PATH_SEPARATOR . SitePath . 'data' . DIRSEP . 'entities';
    set_include_path($includePath);
    
  4. Now, let’s test Zend_Feed component which requires Http, Uri, Validate, Exception and Loader
require_once 'Zend/Feed.php';

// Fetch the latest Slashdot headlines
try {
$phpvnRss = Zend_Feed::import('http://artemis.com.vn/blogvui/?feed=rss2');
} catch (Zend_Feed_Exception $e) {
// feed import failed
echo "Exception caught importing feed: {$e->getMessage()}\n";
exit;
}

// Initialize the channel data array
$channel = array(
'title' => $phpvnRss->title(),
'link' => $phpvnRss->link(),
'description' => $phpvnRss->description(),
'items' => array()
);

// Loop over each channel item and store relevant data
foreach ($phpvnRss as $item) {
$channel['items'][] = array(
'title' => $item->title(),
'link' => $item->link(),
'description' => $item->description()
);
}

if (count($channel['items']))
{
echo "<ul>";
for ($i = 0; $i < 5 && $i < count($channel['items']); $i++)
{
$item = $channel['items'][$i];
echo '<li><a href="' . $item['link'] . '" target="_blank">'.$item['title'].'</a>';
}
echo "</ul>";
}
Tags: , , ,

Leave Comment