《PHP實戰:Laravel手動分頁實現方法詳解》要點:
本文介紹了PHP實戰:Laravel手動分頁實現方法詳解,希望對您有用。如果有疑問,可以聯系我們。
本文實例講述了Laravel手動分頁實現方法.分享給大家供大家參考,具體如下:PHP編程
這里的演示實例基于Laravel的5.2版本PHP編程
在開發過程中有這么一種情況,你請求Java api獲取信息,由于信息較多,需要分頁顯示.Laravel官方提供了一個簡單的方式paginate($perPage),但是這種方法只適用model、查詢構建器.PHP編程
今天說下 給定一個數組如何實現 和paginate方法一樣的效果.PHP編程
查看paginate方法源碼PHP編程
#vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:480 public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null) { $query = $this->toBase(); $total = $query->getCountForPagination(); $this->forPage( $page = $page ?: Paginator::resolveCurrentPage($pageName), $perPage = $perPage ?: $this->model->getPerPage() ); return new LengthAwarePaginator($this->get($columns), $total, $perPage, $page, [ 'path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName, ]); }
從上面就可以看出,分頁的關鍵就在于LengthAwarePaginator.PHP編程
LengthAwarePaginator的構造方法.PHP編程
public function __construct($items, $total, $perPage, $currentPage = null, array $options = []) { foreach ($options as $key => $value) { $this->{$key} = $value; } $this->total = $total; $this->perPage = $perPage; $this->lastPage = (int) ceil($total / $perPage); $this->path = $this->path != '/' ? rtrim($this->path, '/') : $this->path; $this->currentPage = $this->setCurrentPage($currentPage, $this->lastPage); $this->items = $items instanceof Collection ? $items : Collection::make($items); }
其實已經很明白了,假如要分頁的數組為PHP編程
[ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21], ]
共7條數據,每頁顯示3條,共3頁PHP編程
use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Illuminate\Http\Request; # 僅做演示 # function userList(Request $request) { $users = [ ['username'=>'zhangsan', 'age'=>26], ['username'=>'lisi', 'age'=>23], ['username'=>'wangwu', 'age'=>62], ['username'=>'zhaoliu', 'age'=>46], ['username'=>'wangmazi', 'age'=>25], ['username'=>'lanzi', 'age'=>24], ['username'=>'pangzi', 'age'=>21] ]; $perPage = 3; if ($request->has('page')) { $current_page = $request->input('page'); $current_page = $current_page <= 0 ? 1 :$current_page; } else { $current_page = 1; } $item = array_slice($users, ($current_page-1)*$perPage, $perPage); //注釋1 $total = count($users); $paginator =new LengthAwarePaginator($item, $total, $perPage, $currentPage, [ 'path' => Paginator::resolveCurrentPath(), //注釋2 'pageName' => 'page', ]); $userlist = $paginator->toArray()['data']; return view('userlist', compact('userlist', 'paginator')); }
上面的代碼中的重點是$item,如果不做注釋1處理,得出的是所有7條數據.PHP編程
注釋2處就是設定個要分頁的url地址.也可以手動通過 $paginator ->setPath('路徑') 設置.PHP編程
頁面中的分頁連接也是同樣的調用方式:PHP編程
{{ $paginator->render() }}
好了,基本就是這樣,有紕漏的地方歡迎指正!PHP編程
看看最終效果:PHP編程
PHP編程
更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《smarty模板入門基礎教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》PHP編程
希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助.PHP編程
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/3115.html