Skip to content

Commit e90e66a

Browse files
committed
Add Header Link Pagination
1 parent e9e9fdd commit e90e66a

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

application/Config/Pager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class Pager extends BaseConfig
1919
*/
2020
public $templates = [
2121
'default_full' => 'CodeIgniter\Pager\Views\default_full',
22-
'default_simple' => 'CodeIgniter\Pager\Views\default_simple'
22+
'default_simple' => 'CodeIgniter\Pager\Views\default_simple',
23+
'default_header' => 'CodeIgniter\Pager\Views\default_header'
2324
];
2425

2526
/*

system/HTTP/Response.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*/
4646
class RedirectException extends \Exception
4747
{
48-
48+
4949
}
5050

5151
/**
@@ -343,6 +343,23 @@ public function setDate(\DateTime $date)
343343

344344
//--------------------------------------------------------------------
345345

346+
/**
347+
* Set the Link Header
348+
*
349+
* @param \CodeIgniter\Pager\Pager $pager
350+
*
351+
* @return Response
352+
*/
353+
public function setLink(Pager $pager)
354+
{
355+
// http://tools.ietf.org/html/rfc5988
356+
$this->setHeader('Link', $pager->links('default', 'default_header'));
357+
358+
return $this;
359+
}
360+
361+
//--------------------------------------------------------------------
362+
346363
/**
347364
* Sets the Content Type header for this response with the mime type
348365
* and, optionally, the charset.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
$pager->setSurroundCount(0);
3+
$links = '';
4+
if ($pager->hasPrevious())
5+
{
6+
$links .= '<' . $pager->getFirst() . '>; rel="first",';
7+
$links .= '<' . $pager->getPrevious() . '>; rel="prev"';
8+
}
9+
if ($pager->hasPrevious() && $pager->hasNext())
10+
{
11+
$links .= ',';
12+
}
13+
if ($pager->hasNext())
14+
{
15+
$links .= '<' . $pager->getNext() . '>; rel="next",';
16+
$links .= '<' . $pager->getLast() . '>; rel="last"';
17+
}
18+
echo $links;

0 commit comments

Comments
 (0)