[TOC]
## 安装分页组件
~~~
composer require hyperf/paginator
~~~
## 模型分页
~~~
public function index2(RenderInterface $render)
{
$links = Link::query()->paginate(10);
//$links = Link::query()->where('id', '>', 30)->paginate(10);
return $render->render('index.index', ['links' => $links]);
}
~~~
## View中分页显示
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hyperf</title>
</head>
<body>
<ul>
@foreach($links as $link)
<li>{{$link->name}}</li>s
@endforeach
</ul>
{!! \App\Util\CommonUtil::page($links) !!}
</body>
</html>
~~~
## 分页显示助手类
> 路径:App\Util\CommonUtil.php
~~~
declare (strict_types=1);
namespace App\Util;
class CommonUtil{
public static function page($paginator){
$str = '<a href="'.$paginator->url(1).'">首页</a>';
$str .= '<a href="'.$paginator->previousPageUrl().'">上一页</a>';
$str .= '<a href="'.$paginator->nextPageUrl().'">下一页</a>';
$lastPageNum = intval(ceil($paginator->total() / $paginator->perPage())); //尾页数
$str .= '<a href="'.$paginator->url($lastPageNum).'">尾页</a>共:'.$paginator->total().'条';
return $str;
}
}
~~~