This post covers my notes on Adding an RSS Web Feed (chapter 9) in Yii from the book "Web Application Development with Yii and PHP" by Jeffrey Winesett about learning Yii by taking a step-by-step approach to building a Web-based project task tracking system from conception through production deployment - software development life cycle (SDLC) issue-management application.

Feature planning

  • Use Zend Framework in the Yii application
  • Creating a new action in a controller class to respond to the feed request and return the appropriate data in an RSS format
  • Altering our URL structure for ease of use
  • Adding our newly created feed to both the project listings page as well as to each individual project details page
/**
     * Uses Zend Feed to return an RSS formatted comments data feed
     */
    public function actionFeed()
    {
        if(isset($_GET['pid'])) 
        {
            $comments = Comment::model()->with(array(
                                'issue'=>array(
                                    'condition'=>'project_id=:projectId', 
                                    'params'=>array(':projectId'=>intval($_GET['pid'])),
                                )))->recent(20)->findAll();
 
        }
        else   
            $comments = Comment::model()->recent(20)->findAll();  
 
        //convert from an array of comment AR class instances to an name=>value array for Zend
        $entries=array(); 
 
        foreach($comments as $comment)
        {
 
            $entries[]=array(
                    'title'=>CHtml::encode($comment->issue->name),     
                    'link'=>CHtml::encode($this->createAbsoluteUrl('issue/view',array('id'=>$comment->issue->id))),  
                    'description'=> $comment->author->username . ' says:<br>' . $comment->content,
                    'lastUpdate'=>strtotime($comment->create_time),   
                    'author'=>CHtml::encode($comment->author->username),
             );
        }  
 
        //now use the Zend Feed class to generate the Feed
        // generate and render RSS feed
        $feed=Zend_Feed::importArray(array(
             'title'   => 'Trackstar Project Comments Feed',
             'link'    => $this->createAbsoluteUrl(''),
             'charset' => 'UTF-8',
             'entries' => $entries,      
         ), 'rss');
 
        $feed->send();
 
    }
 
/*ProjectController::actionIndex() method.
Alter that method as follows:*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider ('Project');
Yii::app()->clientScript->registerLinkTag(
'alternate',
'application/rss+xml',
$this->createUrl('comment/feed'));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
 
/*similar change to add this link to a specific project details page. The rendering of these pages is handled by the ProjectController::actionView() method.*/
 
    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($id)
    {
        $issueDataProvider=new CActiveDataProvider('Issue', array(
            'criteria'=>array(
                'condition'=>'project_id=:projectId',
                'params'=>array(':projectId'=>$this->loadModel($id)->id),
            ),
            'pagination'=>array(
                'pageSize'=>1,
            ),
         ));
 
        Yii::app()->clientScript->registerLinkTag(
            'alternate',
            'application/rss+xml',
            $this->createUrl('comment/feed',array('pid'=>$this->loadModel($id)->id)));
 
 
        $this->render('view',array(
            'model'=>$this->loadModel($id),
            'issueDataProvider'=>$issueDataProvider,
        ));
 
    }

Download source code of chapter 9

Chapter 10 covers: Adding Layout (looking good)

Download source code of chapter 10

Chapter 11 covers: Using Yii Modules

Download source code of chapter 11

Chapter 12 covers: Production Readiness

Download source code of chapter 12

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.