Creating Recent Comments Portlet

In this section, we create the last portlet that displays a list of comments recently published.

1. Creating RecentComments Class

We create the RecentComments class in the file /wwwroot/blog/protected/components/RecentComments.php. The file has the following content:

class RecentComments extends Portlet
    public $title='Recent Comments';
    public function getRecentComments()
        return Comment::model()->findRecentComments();
    protected function renderContent()

In the above we invoke the findRecentComments method which is defined in the Comment class as follows,

class Comment extends CActiveRecord
    public function findRecentComments($limit=10)
            'order'=>'Comment.createTime DESC',
        return $this->with('post')->findAll($criteria);

2. Creating recentComments View

The recentComments view is saved in the file /wwwroot/blog/protected/components/views/recentComments.php. The view simply displays every comment returned by the RecentComments::getRecentComments() method.

3. Using RecentComments Portlet

We modify the layout file /wwwroot/blog/protected/views/layouts/main.php to embed this last portlet,

<div id="sidebar">
<?php $this->widget('UserLogin',array('visible'=>Yii::app()->user->isGuest)); ?>
<?php $this->widget('UserMenu',array('visible'=>!Yii::app()->user->isGuest)); ?>
<?php $this->widget('TagCloud'); ?>
<?php $this->widget('RecentComments'); ?>
$Id: portlet.comments.txt 671 2009-02-13 21:55:24Z qiang.xue $

Be the first person to leave a comment

Please to leave your comment.