一、创建手动分页
在laravel自带的分页中,一般是通过数据库查询访问paginate()方法来达到分页的效果 ,like this:
class IndexControllerextends Controller
{
publicfunctionindex()
{
$person = DB::table('person')->paginate(15);
return view('index.pagTest',['person'=> $person]);
}
}查看框架的分页源代码
#vender/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
/**
* Paginate the given query.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*
* @throws \InvalidArgumentException
*/
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$perPage = $perPage ?: $this->model->getPerPage();
$results = ($total = $this->toBase()->getCountForPagination())
? $this->forPage($page, $perPage)->get($columns)
: $this->model->newCollection();
return $this->paginator($results, $total, $perPage, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
}发现,分页用了 \Illuminate\Contracts\Pagination\LengthAwarePaginator 构造方法,查看这个构造方法
$value) {
$this->{$key} = $value;
}
$this->total = $total;
$this->perPage = $perPage;
$this->lastPage = max((int) ceil($total / $perPage), 1);
$this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
$this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
$this->items = $items instanceof Collection ? $items : Collection::make($items);
}如果要实现手动分页,只需要使用这个构造方法,给定参数,就能达到分页的效果
贴代码:
public function setPage2(Request $request,$data,$prepage,$total){
#每页显示记录
$prePage = $prepage;
//$total =count($data);
$allitem = $prepage *100;
$total > $allitem ? $total = $allitem : $total;
if(isset($request->page)){
$current_page =intval($request->page);
$current_page =$current_page<=0?1:$current_page;
}else{
$current_page = 1;
}
#url操作
$url = $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"];
if(strpos($url,'&page')) $url=str_replace('&page='.$request->page, '',$url);
# $data must be array
$item =array_slice($data,($current_page-1)*$prePage,$prePage);
$paginator = new LengthAwarePaginator($item,$total,$prePage,$current_page,[
'path'=>$url,
'pageName'=>'page'
]);
return $paginator;
}($data 为需要进行分页的数据)
说明:
1、在考虑到代码的复用性,我将分页代码封装到app/Controllers/Controller.php中的一个方法里面,这样在其他控制器里只需要$this->setPage(Request $request,$data,$prepage,$total) 就能使用了,(前提:其他控制器继承了Controller.php)
2、分页的URL,因为我的项目的url一定会携带一个kw参数,所以我直接用str_replace替换"&page",如果是存在不携参分页的话,需要判断,到底是"?page"还是"&page"。(url的逻辑可以自己写)
#分页 php
$paginator = $this->setPage2($request,$data,25,$sum); $data =$paginator->toArray()['data'];
在模板中:{{$paginator->render()}}即能输出分页HTML,样式如下:

二、自定义分页样式
在实际开发中,不希望用户在浏览时直接浏览最后几页,只想用户从前往后依次的浏览,如百度搜索分页,这时候,就想修改分页的样式,经过一个下午的奋战,贴出解决过程
在上一环节中,手动创建了分页,了解HTML的模板生成是render()方法,
#\Illuminate\Contracts\Pagination\LengthAwarePaginator
/**
* Render the paginator using the given view.
*
* @param string|null $view
* @param array $data
* @return \Illuminate\Support\HtmlString
*/
public function render($view = null, $data = [])
{
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
'paginator' => $this,
'elements' => $this->elements(),
]))->render());
}经过思考,我们不去改laravel框架的源代码,可以通过重构render方法或者重新定义一个生成HTML模板的方法来实现自定义HTML模板
因为我们只需要自定义HTML模板,所以,可以创建一个文件,继承\Illuminate\Contracts\Pagination\LengthAwarePaginator 类
看代码:
hasPages())
{
return sprintf("我选择的方法是自定义新的方法生成HTML模板,模板中通过:{{$paginator->newrender()}}输出HTML
如果选择重构render()方法,只需要将上面的newrender()方法做一些小变动
public function render($view=null,$data=[]){
if($this->hasPages())
{
return sprintf("模板中通过:{{$paginator->render()}}输出HTML
最终效果如图:
