2016-05-18 10:49:30 0 评论 Yii 2.0 Boy.Lee

Yii 2.0 基于MVC分页实例

分页在日常Web开发中是一个常用的技巧, 今天就展示一下如何在Yii 2.0框架中实现基于MVC的标准分页代码.

{ Controller }

//get total topic count
$query = Topic::find()->where(['t_status_id'=>Topic::STATUS_PUBLISH]);
$count = $query->count();

//pagination
$pagination = new Pagination(['totalCount'=>$count, 'pageSize'=>20]);

//load page level topics
$query = Topic::find()->where(['t_status_id'=>Topic::STATUS_PUBLISH]);

$topics = $query->offset($pagination->offset)
                ->limit($pagination->limit)
                ->orderBy('t_id DESC')
                ->all();


return $this->render('index', [
        'topics' => $topics,
        'pagination' => $pagination
]);

 

{ View }

<?php foreach ($topics as $oneTopic):?>

	//TODO render topic detail

<?php endforeach;?>


<!-- Start Pagination -->
<?= LinkPager::widget([
    'pagination' => $pagination,
    'options'=>['class'=>'leePage pagination'],
]);; ?>
<!-- End Pagination -->