본문 바로가기

Yii Framework/블로그 만들기

18. 최근 코멘트 포틀릿 생성

이 곳은 제가 개인적으로 YII framework의 블로그 만들기를 번역해 놓은 곳입니다.

제가 영어 전공자도.. 그렇다고 영어랑 친하지도 않습니다. 

그래서 보시면 뭔가 글도 엉성하고 말이 안맞는게 많습니다.

잘못 오역된 부분이라던지 그런 부분들 친절하게 알려주시면 바로 수정하겠습니다.

이 페지의 원글 http://www.yiiframework.com/doc/blog/1.1/en/portlet.comments



최근 코멘트 포틀릿 생성 

  1. RecentComments 클래스 만들기
  2. recentComments 보기 만들기
  3. RecentComments 포틀릿 사용

이 섹션에서는 최근 등록 된 댓글 목록보기, 마지막 포틀릿을 만듭니다.

1. RecentComments 클래스 만들기
/wwwroot/blog/protected/components/RecentComments.php 파일 RecentComments 클래스를 만듭니다.이 파일은 다음의 내용입니다.

Yii::import('zii.widgets.CPortlet');
 
class RecentComments extends CPortlet
{
    public $title='Recent Comments';
    public $maxComments=10;
 
    public function getRecentComments()
    {
        return Comment::model()->findRecentComments($this->maxComments);
    }
 
    protected function renderContent()
    {
        $this->render('recentComments');
    }
}

위에서 호출하는 findRecentComments 메소드는 Comment 클래스에서 다음과 같이 정의됩니다.

class Comment extends CActiveRecord
{
    ......
    public function findRecentComments($limit=10)
    {
        return $this->with('post')->findAll(array(
            'condition'=>'t.status='.self::STATUS_APPROVED,
            'order'=>'t.create_time DESC',
            'limit'=>$limit,
        ));
    }
}

2. recentComments 보기 만들기

recentComments 보기 /wwwroot/blog/protected/components/views/recentComments.php 파일로 저장합니다. 이것은 단순히 RecentComments::getRecentComments() 메소드로 반환되는 의견 하나 하나를 표시합니다.

3. RecentComments 포틀릿 사용

 레이아웃 파일 /wwwroot/blog/protected/views/layouts/column2.php 수정이 포틀릿을 포함합니다.

......
<div id="sidebar">
 
    <?php if(!Yii::app()->user->isGuest) $this->widget('UserMenu'); ?>
 
    <?php $this->widget('TagCloud', array(
        'maxTags'=>Yii::app()->params['tagCloudCount'],
    )); ?>
 
    <?php $this->widget('RecentComments', array(
        'maxComments'=>Yii::app()->params['recentCommentCount'],
    )); ?>
 
</div>
......