接上一篇 基于yii2的blog系统开发6:

第十三步 评论管理页面的完善

5.1video_url:https://v.youku.com/v_show/id_XMTc2MzEzNzkxNg==.html

1.GridView里面content内容缩短显示

方法一(不推荐):

修改backend/views/comment/index.php:

['attribute'=>'content',
                'value'=>function($model){
                    $ts = strip_tags($model->content); //去掉html标签
                    $tlen = mb_strlen($ts);
                    return mb_substr($ts,0,5,'utf-8').($tlen>5?'...':'');
                }
            ],

方法二(推荐):在comment模型里面添加一个mycontent的只读属性

修改backend/views/comment/index.php:

 ['attribute'=>'content',
                'value'=>'mycontent',
            ],

修改common/models/comment.php:

public function getMycontent()
    {
        $ts = strip_tags($this->content); //去掉html标签
        $tlen = mb_strlen($ts);
        return mb_substr($ts,0,5,'utf-8').($tlen>5?'...':'');
    }

php小知识:

*在strlen计算时,对待一个UTF8的中文字符是3个长度,所以'中文a字1符'长度是3x4+2=14;在mb_strlen计算时,选定内码为UTF8,则会将一个中文字符当作长度1来计算,所以'中文a字1符'长度是6. *

2.添加评论审核功能

1.修改视图文件backend/views/comment/index.php:

['class' => 'yii\grid\ActionColumn',
                'template'=>'{view}{update}{delete}{approve}',
                'buttons'=>[
                    'approve'=>function($url,$model,$key){
                        $options=[
                            'title'=>Yii::t('yii','审核'),
                            'aria-label'=>Yii::t('yii','审核'),
                            'data-confirm'=>Yii::t('yii','确定审核通过吗?'),
                            'data-method'=>'post',
                            'data-pjax'=>'0',
                        ];
                        return Html::a('<span class="glyphicon glyphicon-check"></span>',$url,$options);
                    },
                ],

            ],

2.修改comment控制器和模型类

//backend/controllers/CommentController.php
public function actionApprove($id)
    {
        $m = $this->findModel($id);
        if($m->approve())
        {
            return $this->redirect(['index']);
        }
    }
//common/models/Comment.php
public function approve()
    {
        $this->status = 2;
        return ($this->save()?true:false);
    }

3.导航栏提示待评论审核气泡

//1.modify common/models/Comment.php
public static function getCcount()
    {
        return Comment::find()->where(['status'=>1])->count();
    }
//2.modify backend/views/layouts/main.php
 ['label' => '评论管理', 'url' => ['/comment/index']],
        '<li><span class="badge badge-inverse">'.Comment::getCcount().'</span></li>',