|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\PaginateRoute; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Router; |
|
6
|
|
|
use Illuminate\Translation\Translator; |
|
7
|
|
|
use Illuminate\Support\Facades\Request; |
|
8
|
|
|
use Illuminate\Routing\RouteParameterBinder; |
|
9
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
10
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
|
11
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
|
12
|
|
|
|
|
13
|
|
|
class PaginateRoute |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Illuminate\Translation\Translator |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $translator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Illuminate\Routing\Router |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $router; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Illuminate\Contracts\Routing\UrlGenerator |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $urlGenerator; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $pageKeyword; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \Illuminate\Translation\Translator $translator |
|
37
|
|
|
* @param \Illuminate\Routing\Router $router |
|
38
|
|
|
* @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Translator $translator, Router $router, UrlGenerator $urlGenerator) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->translator = $translator; |
|
43
|
|
|
$this->router = $router; |
|
44
|
|
|
$this->urlGenerator = $urlGenerator; |
|
45
|
|
|
|
|
46
|
|
|
// Unfortunately we can't do this in the service provider since routes are booted first |
|
47
|
|
|
$this->translator->addNamespace('paginateroute', __DIR__.'/../resources/lang'); |
|
48
|
|
|
|
|
49
|
|
|
$this->pageKeyword = $this->translator->get('paginateroute::paginateroute.page'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Return the current page. |
|
54
|
|
|
* |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
public function currentPage() |
|
58
|
|
|
{ |
|
59
|
|
|
$currentRoute = $this->router->getCurrentRoute(); |
|
60
|
|
|
|
|
61
|
|
|
if (! $currentRoute) { |
|
62
|
|
|
return 1; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$query = $currentRoute->parameter('pageQuery'); |
|
66
|
|
|
|
|
67
|
|
|
return (int) str_replace($this->pageKeyword.'/', '', $query) ?: 1; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if the given page is the current page. |
|
72
|
|
|
* |
|
73
|
|
|
* @param int $page |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public function isCurrentPage($page) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->currentPage() === $page; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the next page number. |
|
84
|
|
|
* |
|
85
|
|
|
* @param \Illuminate\Contracts\Pagination\Paginator $paginator |
|
86
|
|
|
* |
|
87
|
|
|
* @return string|null |
|
88
|
|
|
*/ |
|
89
|
|
|
public function nextPage(Paginator $paginator) |
|
90
|
|
|
{ |
|
91
|
|
|
if (! $paginator->hasMorePages()) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->currentPage() + 1; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine wether there is a next page. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Illuminate\Contracts\Pagination\Paginator $paginator |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function hasNextPage(Paginator $paginator) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->nextPage($paginator) !== null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get the next page URL. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Illuminate\Contracts\Pagination\Paginator $paginator |
|
114
|
|
|
* |
|
115
|
|
|
* @return string|null |
|
116
|
|
|
*/ |
|
117
|
|
|
public function nextPageUrl(Paginator $paginator) |
|
118
|
|
|
{ |
|
119
|
|
|
$nextPage = $this->nextPage($paginator); |
|
120
|
|
|
|
|
121
|
|
|
if ($nextPage === null) { |
|
122
|
|
|
return; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $this->pageUrl($nextPage); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the previous page number. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string|null |
|
132
|
|
|
*/ |
|
133
|
|
|
public function previousPage() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($this->currentPage() <= 1) { |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->currentPage() - 1; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Determine wether there is a previous page. |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function hasPreviousPage() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->previousPage() !== null; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the previous page URL. |
|
154
|
|
|
* |
|
155
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
156
|
|
|
* Ex. /users/page/1 instead of /users |
|
157
|
|
|
* |
|
158
|
|
|
* @return string|null |
|
159
|
|
|
*/ |
|
160
|
|
|
public function previousPageUrl($full = false) |
|
161
|
|
|
{ |
|
162
|
|
|
$previousPage = $this->previousPage(); |
|
163
|
|
|
|
|
164
|
|
|
if ($previousPage === null) { |
|
165
|
|
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $this->pageUrl($previousPage, $full); |
|
169
|
|
|
} |
|
170
|
|
|
/** |
|
171
|
|
|
* Get all urls in an array. |
|
172
|
|
|
* |
|
173
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
|
174
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
175
|
|
|
* Ex. /users/page/1 instead of /users |
|
176
|
|
|
* |
|
177
|
|
|
* @return array |
|
178
|
|
|
*/ |
|
179
|
|
|
public function allUrls(LengthAwarePaginator $paginator, $full = false) |
|
180
|
|
|
{ |
|
181
|
|
|
if (! $paginator->hasPages()) { |
|
182
|
|
|
return []; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$urls = []; |
|
186
|
|
|
$side = $paginator->onEachSide; |
|
|
|
|
|
|
187
|
|
|
$current = $paginator->currentPage(); |
|
188
|
|
|
$last = $paginator->lastPage(); |
|
189
|
|
|
|
|
190
|
|
|
if (!empty($side)) { |
|
191
|
|
|
$total = $current + $side; |
|
192
|
|
|
$first = $current - $side; |
|
193
|
|
|
if ($first < 1) { |
|
194
|
|
|
$first = 1; |
|
195
|
|
|
$total += $side; |
|
196
|
|
|
} |
|
197
|
|
|
if ($total > $last) { |
|
198
|
|
|
$total = $last; |
|
199
|
|
|
if ($first > $side + 1) { |
|
200
|
|
|
$first -= $side; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
else { |
|
205
|
|
|
$first = 1; |
|
206
|
|
|
$total = $last; |
|
207
|
|
|
} |
|
208
|
|
|
for ($page = $first; $page <= $total; $page++) { |
|
209
|
|
|
$urls[$page] = $this->pageUrl($page, $full); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $urls; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Render a plain html list with previous, next and all urls. The current page gets a current class on the list item. |
|
217
|
|
|
* |
|
218
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
|
219
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
220
|
|
|
* Ex. /users/page/1 instead of /users |
|
221
|
|
|
* @param string $class Include class on pagination list |
|
222
|
|
|
* Ex. <ul class="pagination"> |
|
223
|
|
|
* @param bool $additionalLinks Include prev and next links on pagination list |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
*/ |
|
227
|
|
|
public function renderPageList(LengthAwarePaginator $paginator, $full = false, $class = null, $additionalLinks = false) |
|
228
|
|
|
{ |
|
229
|
|
|
$urls = $this->allUrls($paginator, $full); |
|
230
|
|
|
|
|
231
|
|
|
if ($class) { |
|
|
|
|
|
|
232
|
|
|
$class = " class=\"$class\""; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$listItems = "<ul{$class}>"; |
|
236
|
|
|
|
|
237
|
|
|
if ($this->hasPreviousPage() && $additionalLinks) { |
|
238
|
|
|
$listItems .= "<li><a href=\"{$this->previousPageUrl()}\">«</a></li>"; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
foreach ($urls as $i => $url) { |
|
242
|
|
|
$pageNum = $i; |
|
243
|
|
|
$css = ''; |
|
244
|
|
|
|
|
245
|
|
|
$link = "<a href=\"{$url}\">{$pageNum}</a>"; |
|
246
|
|
|
if ($pageNum == $this->currentPage()) { |
|
247
|
|
|
$css = ' class="active"'; |
|
248
|
|
|
$link = $pageNum; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$listItems .= "<li{$css}>$link</li>"; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
if ($this->hasNextPage($paginator) && $additionalLinks) { |
|
255
|
|
|
$listItems .= "<li><a href=\"{$this->nextPageUrl($paginator)}\">»</a></li>"; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$listItems .= '</ul>'; |
|
259
|
|
|
|
|
260
|
|
|
return $listItems; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Render html link tags for SEO indication of previous and next page. |
|
265
|
|
|
* |
|
266
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
|
267
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
268
|
|
|
* Ex. /users/page/1 instead of /users |
|
269
|
|
|
* |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function renderRelLinks(LengthAwarePaginator $paginator, $full = false) |
|
273
|
|
|
{ |
|
274
|
|
|
$urls = $this->allUrls($paginator, $full); |
|
275
|
|
|
|
|
276
|
|
|
$linkItems = ''; |
|
277
|
|
|
|
|
278
|
|
|
foreach ($urls as $i => $url) { |
|
279
|
|
|
$pageNum = $i + 1; |
|
280
|
|
|
|
|
281
|
|
|
switch ($pageNum - $this->currentPage()) { |
|
282
|
|
|
case -1: |
|
283
|
|
|
$linkItems .= "<link rel=\"prev\" href=\"{$url}\" />"; |
|
284
|
|
|
break; |
|
285
|
|
|
case 1: |
|
286
|
|
|
$linkItems .= "<link rel=\"next\" href=\"{$url}\" />"; |
|
287
|
|
|
break; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
return $linkItems; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* @deprecated in favor of renderPageList. |
|
296
|
|
|
* |
|
297
|
|
|
* @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator |
|
298
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
299
|
|
|
* Ex. /users/page/1 instead of /users |
|
300
|
|
|
* |
|
301
|
|
|
* @return string |
|
302
|
|
|
*/ |
|
303
|
|
|
public function renderHtml(LengthAwarePaginator $paginator, $full = false) |
|
304
|
|
|
{ |
|
305
|
|
|
return $this->renderPageList($paginator, $full); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Generate a page URL, based on the request's current URL. |
|
310
|
|
|
* |
|
311
|
|
|
* @param int $page |
|
312
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
313
|
|
|
* Ex. /users/page/1 instead of /users |
|
314
|
|
|
* |
|
315
|
|
|
* @return string |
|
316
|
|
|
*/ |
|
317
|
|
|
public function pageUrl($page, $full = false) |
|
318
|
|
|
{ |
|
319
|
|
|
$currentPageUrl = $this->router->getCurrentRoute()->uri(); |
|
320
|
|
|
|
|
321
|
|
|
$url = $this->addPageQuery(str_replace('{pageQuery?}', '', $currentPageUrl), $page, $full); |
|
322
|
|
|
|
|
323
|
|
|
foreach ((new RouteParameterBinder($this->router->getCurrentRoute()))->parameters(app('request')) as $parameterName => $parameterValue) { |
|
324
|
|
|
$url = str_replace(['{'.$parameterName.'}', '{'.$parameterName.'?}'], $parameterValue, $url); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
$query = Request::getQueryString(); |
|
328
|
|
|
|
|
329
|
|
|
$query = $query |
|
330
|
|
|
? '?'.$query |
|
331
|
|
|
: ''; |
|
332
|
|
|
|
|
333
|
|
|
return $this->urlGenerator->to($url).$query; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* Append the page query to a URL. |
|
338
|
|
|
* |
|
339
|
|
|
* @param string $url |
|
340
|
|
|
* @param int $page |
|
341
|
|
|
* @param bool $full Return the full version of the URL in for the first page |
|
342
|
|
|
* Ex. /users/page/1 instead of /users |
|
343
|
|
|
* |
|
344
|
|
|
* @return string |
|
345
|
|
|
*/ |
|
346
|
|
|
public function addPageQuery($url, $page, $full = false) |
|
347
|
|
|
{ |
|
348
|
|
|
// If the first page's URL is requested and $full is set to false, there's nothing to be added. |
|
349
|
|
|
if ($page === 1 && ! $full) { |
|
350
|
|
|
return $url; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
return trim($url, '/')."/{$this->pageKeyword}/{$page}"; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Register the Route::paginate macro. |
|
358
|
|
|
*/ |
|
359
|
|
|
public function registerMacros() |
|
360
|
|
|
{ |
|
361
|
|
|
$pageKeyword = $this->pageKeyword; |
|
362
|
|
|
$router = $this->router; |
|
363
|
|
|
|
|
364
|
|
|
$router->macro('paginate', function ($uri, $action) use ($pageKeyword, $router) { |
|
365
|
|
|
$route = null; |
|
366
|
|
|
|
|
367
|
|
|
$router->group( |
|
368
|
|
|
['middleware' => 'Spatie\PaginateRoute\SetPageMiddleware'], |
|
369
|
|
|
function () use ($pageKeyword, $router, $uri, $action, &$route) { |
|
370
|
|
|
$route = $router->get($uri.'/{pageQuery?}', $action)->where('pageQuery', $pageKeyword.'/[0-9]+'); |
|
371
|
|
|
}); |
|
372
|
|
|
|
|
373
|
|
|
return $route; |
|
374
|
|
|
}); |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: