|
| 1 | +<p align="center"> |
| 2 | +<img width="320" alt="lampager-cakephp" src="https://user-images.githubusercontent.com/1351893/31820647-42c45c7a-b5dd-11e7-9ac8-f1000e961662.png"> |
| 3 | +</p> |
| 4 | +<p align="center"> |
| 5 | +<a href="https://travis-ci.com/lampager/lampager-cakephp"><img src="https://travis-ci.com/lampager/lampager-cakephp.svg?branch=master" alt="Build Status"></a> |
| 6 | +<a href="https://coveralls.io/github/lampager/lampager-cakephp?branch=master"><img src="https://coveralls.io/repos/github/lampager/lampager-cakephp/badge.svg?branch=master" alt="Coverage Status"></a> |
| 7 | +<a href="https://scrutinizer-ci.com/g/lampager/lampager-cakephp/?branch=master"><img src="https://scrutinizer-ci.com/g/lampager/lampager-cakephp/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a> |
| 8 | +</p> |
| 9 | + |
| 10 | +# Lampager for CakePHP |
| 11 | + |
| 12 | +Rapid pagination without using OFFSET |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- PHP: ^5.6 || ^7.0 |
| 17 | +- CakePHP: ^3.6 |
| 18 | +- [lampager/lampager](https://github.com/lampager/lampager): ^0.4 |
| 19 | + |
| 20 | +## Installing |
| 21 | + |
| 22 | +```bash |
| 23 | +composer require lampager/lampager-cakephp |
| 24 | +``` |
| 25 | + |
| 26 | +## Basic Usage |
| 27 | + |
| 28 | +The `Plugin` is not provided. Simply install as a Composer package and use in |
| 29 | +one of the following methods: |
| 30 | + |
| 31 | +- Use in Controller (via `Lampager\Cake\Datasource\Paginator`) |
| 32 | +- Use in Table (via `Lampager\Cake\Model\Behavior\LampagerBehavior`) |
| 33 | + |
| 34 | +### Use in Controller |
| 35 | + |
| 36 | +At first, load the default Paginator component with the Lampager `Paginator` in |
| 37 | +your Controller class (`AppController` is preferable). |
| 38 | + |
| 39 | +Accept cursor parameters from a request and pass it through |
| 40 | +`PaginatorComponent::paginate()`. The `Paginator` also accepts `Query` |
| 41 | +expression that is created by `Table` classes. |
| 42 | + |
| 43 | +```php |
| 44 | +namespace App\Controller; |
| 45 | + |
| 46 | +use Cake\Controller\Controller; |
| 47 | +use Lampager\Cake\Datasource\Paginator; |
| 48 | + |
| 49 | +class AppController extends Controller |
| 50 | +{ |
| 51 | + public function initialize() |
| 52 | + { |
| 53 | + parent::initialize(); |
| 54 | + |
| 55 | + $this->loadComponent('Paginator', [ |
| 56 | + 'paginator' => new Paginator(), |
| 57 | + ]); |
| 58 | + } |
| 59 | + |
| 60 | + public function index() |
| 61 | + { |
| 62 | + // Get cursor parameters |
| 63 | + $previous = json_decode($this->request->getQuery('previous_cursor'), true); |
| 64 | + $next = json_decode($this->request->getQuery('next_cursor'), true); |
| 65 | + $cursor = $previous ?: $next ?: []; |
| 66 | + |
| 67 | + /** @var \Lampager\PaginationResult<\Cake\ORM\Entity> $posts */ |
| 68 | + $posts = $this->paginate('Posts', [ |
| 69 | + // Lampager options |
| 70 | + // If the previous_cursor is not set, paginate forward; otherwise backward |
| 71 | + 'forward' => !$previous, |
| 72 | + 'cursor' => $cursor, |
| 73 | + 'seekable' => true, |
| 74 | + |
| 75 | + // PaginatorComponent config |
| 76 | + 'order' => [ |
| 77 | + 'created' => 'DESC', |
| 78 | + 'id' => 'DESC', |
| 79 | + ], |
| 80 | + 'limit' => 15, |
| 81 | + ]); |
| 82 | + $this->set('posts', $posts); |
| 83 | + } |
| 84 | + |
| 85 | + public function query() |
| 86 | + { |
| 87 | + // Get cursor parameters |
| 88 | + $previous = json_decode($this->request->getQuery('previous_cursor'), true); |
| 89 | + $next = json_decode($this->request->getQuery('next_cursor'), true); |
| 90 | + $cursor = $previous ?: $next ?: []; |
| 91 | + |
| 92 | + // Query expression can also be passed to PaginatorComponent::paginate() as normal |
| 93 | + $query = TableRegistry::getTableLocator()->get('Posts') |
| 94 | + ->where(['Posts.type' => 'public']) |
| 95 | + ->orderDesc('created') |
| 96 | + ->orderDesc('id') |
| 97 | + ->limit(15); |
| 98 | + |
| 99 | + /** @var \Lampager\PaginationResult<\Cake\ORM\Entity> $posts */ |
| 100 | + $posts = $this->paginate($query, [ |
| 101 | + // If the previous_cursor is not set, paginate forward; otherwise backward |
| 102 | + 'forward' => !$previous, |
| 103 | + 'cursor' => $cursor, |
| 104 | + 'seekable' => true, |
| 105 | + ]); |
| 106 | + $this->set('posts', $posts); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +And the pagination links can be output as follows: |
| 112 | + |
| 113 | +```php |
| 114 | +// If there is a next page, print pagination link |
| 115 | +if ($posts->hasPrevious) { |
| 116 | + echo $this->Html->link('<< Previous', [ |
| 117 | + 'controller' => 'posts', |
| 118 | + 'action' => 'index', |
| 119 | + '?' => [ |
| 120 | + 'previous_cursor' => json_encode($posts->previousCursor), |
| 121 | + ], |
| 122 | + ]); |
| 123 | +} |
| 124 | + |
| 125 | +// If there is a next page, print pagination link |
| 126 | +if ($posts->hasNext) { |
| 127 | + echo $this->Html->link('Next >>', [ |
| 128 | + 'controller' => 'posts', |
| 129 | + 'action' => 'index', |
| 130 | + '?' => [ |
| 131 | + 'next_cursor' => json_encode($posts->nextCursor), |
| 132 | + ], |
| 133 | + ]); |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### Use in Table |
| 138 | + |
| 139 | +Initialize `LampagerBehavior` in your Table class (`AppTable` is preferable) |
| 140 | +and simply use `lampager()` from the `Table` class. The query builder extends |
| 141 | +the plain old `\Cake\ORM\Query` and is mixed in with `\Lampager\Paginator`. Note |
| 142 | +that some of the methods in `\Lampager\Paginator`, viz., `orderBy()`, |
| 143 | +`orderByDesc()`, and `clearOrderBy()` are not exposed because their method |
| 144 | +signatures are not compatible with the CakePHP query builder. |
| 145 | + |
| 146 | +```php |
| 147 | +namespace App\Model\Table; |
| 148 | + |
| 149 | +use Cake\ORM\Table; |
| 150 | +use Lampager\Cake\Model\Behavior\LampagerBehavior; |
| 151 | + |
| 152 | +class AppTable extends Table |
| 153 | +{ |
| 154 | + public function initialize(array $config) |
| 155 | + { |
| 156 | + parent::initialize($config); |
| 157 | + |
| 158 | + $this->addBehavior(LampagerBehavior::class); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @return \Lampager\PaginationResult |
| 163 | + */ |
| 164 | + public function latest(array $cursor = []) |
| 165 | + { |
| 166 | + return $this->lampager() |
| 167 | + ->forward() |
| 168 | + ->seekable() |
| 169 | + ->cursor($cursor) |
| 170 | + ->limit(10) |
| 171 | + ->orderDesc('Post.modified') |
| 172 | + ->orderDesc('Post.created') |
| 173 | + ->orderDesc('Post.id'); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @return \Lampager\PaginationResult |
| 178 | + */ |
| 179 | + public function draft(array $cursor = []) |
| 180 | + { |
| 181 | + // The methods from the CakePHP query builder, e.g., where(), are available. |
| 182 | + // \Cake\Database\Expression\QueryExpression can be accepted as well. |
| 183 | + return $this->lampager() |
| 184 | + ->where(['type' => 'draft']) |
| 185 | + ->forward() |
| 186 | + ->seekable() |
| 187 | + ->cursor($cursor) |
| 188 | + ->limit(10) |
| 189 | + ->orderDesc($this->query()->newExpr('modified')) |
| 190 | + ->orderDesc($this->query()->newExpr('created')) |
| 191 | + ->orderDesc($this->query()->newExpr('id')); |
| 192 | + } |
| 193 | +} |
| 194 | +``` |
0 commit comments