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