Paginator
class Cake\View\Helper\PaginatorHelper(View $view, array $config = [])
The PaginatorHelper is used to output pagination controls such as page numbers and next/previous links. It works in tandem with PaginatorComponent.
See also Pagination for information on how to create paginated datasets and do paginated queries.
PaginatorHelper Templates
Internally PaginatorHelper uses a series of simple HTML templates to generate markup. You can modify these templates to customize the HTML generated by the PaginatorHelper.
Templates use style placeholders. It is important to not add any spaces around the or the replacements will not work.
Loading Templates from a File
When adding the PaginatorHelper in your controller, you can define the 'templates' setting to define a template file to load. This allows you to customize multiple templates and keep your code DRY:
// In your AppView.php
public function initialize()
{
...
$this->loadHelper('Paginator', ['templates' => 'paginator-templates']);
}This will load the file located at config/paginator-templates.php. See the example below for how the file should look like. You can also load templates from a plugin using plugin syntax:
// In your AppView.php
public function initialize()
{
...
$this->loadHelper('Paginator', ['templates' => 'MyPlugin.paginator-templates']);
}Whether your templates are in the primary application or a plugin, your templates file should look something like:
return [
'number' => '<a href="{{url}}">{{text}}</a>',
];Changing Templates at Run-time
method Cake\View\Helper\PaginatorHelper::setTemplates($templates)
This method allows you to change the templates used by PaginatorHelper at runtime. This can be useful when you want to customize templates for a particular method call:
// Read the current template value.
$result = $this->Paginator->getTemplates('number');
// Prior to 3.4
$result = $this->Paginator->templates('number');
// Change a template
$this->Paginator->setTemplates([
'number' => '<em><a href="{{url}}">{{text}}</a></em>'
]);WARNING
Template strings containing a percentage sign (%) need special attention, you should prefix this character with another percentage so it looks like %%. The reason is that internally templates are compiled to be used with sprintf(). Example: '<div style="width:%%"></div>'
Template Names
PaginatorHelper uses the following templates:
nextActiveThe active state for a link generated by next().nextDisabledThe disabled state for next().prevActiveThe active state for a link generated by prev().prevDisabledThe disabled state for prev()counterRangeThe template counter() uses when format == range.counterPagesThe template counter() uses when format == pages.firstThe template used for a link generated by first().lastThe template used for a link generated by last()numberThe template used for a link generated by numbers().currentThe template used for the current page.ellipsisThe template used for ellipses generated by numbers().sortThe template for a sort link with no direction.sortAscThe template for a sort link with an ascending direction.sortDescThe template for a sort link with a descending direction.
Creating Sort Links
method Cake\View\Helper\PaginatorHelper::sort($key, $title = null, $options = [])
Generates a sorting link. Sets querystring parameters for the sort and direction. Links will default to sorting by asc. After the first click, links generated with sort() will handle direction switching automatically. If the resultset is sorted 'asc' by the specified key the returned link will sort by 'desc'.
Accepted keys for $options:
escapeWhether you want the contents HTML entity encoded, defaults totrue.modelThe model to use, defaults toPaginatorHelper::defaultModel().directionThe default direction to use when this link isn't active.lockLock direction. Will only use the default direction then, defaults tofalse.
Assuming you are paginating some posts, and are on page one:
echo $this->Paginator->sort('user_id');Output:
<a href="/posts/index?page=1&sort=user_id&direction=asc">User Id</a>You can use the title parameter to create custom text for your link:
echo $this->Paginator->sort('user_id', 'User account');Output:
<a href="/posts/index?page=1&sort=user_id&direction=asc">User account</a>If you are using HTML like images in your links remember to set escaping off:
echo $this->Paginator->sort(
'user_id',
'<em>User account</em>',
['escape' => false]
);Output:
<a href="/posts/index?page=1&sort=user_id&direction=asc"><em>User account</em></a>The direction option can be used to set the default direction for a link. Once a link is active, it will automatically switch directions like normal:
echo $this->Paginator->sort('user_id', null, ['direction' => 'desc']);Output:
<a href="/posts/index?page=1&sort=user_id&direction=desc">User Id</a>The lock option can be used to lock sorting into the specified direction:
echo $this->Paginator->sort('user_id', null, ['direction' => 'asc', 'lock' => true]);method Cake\View\Helper\PaginatorHelper::sortDir(string $model = null, mixed $options = [])
method Cake\View\Helper\PaginatorHelper::sortKey(string $model = null, mixed $options = [])
Creating Page Number Links
method Cake\View\Helper\PaginatorHelper::numbers($options = [])
Returns a set of numbers for the paged result set. Uses a modulus to decide how many numbers to show on each side of the current page By default 8 links on either side of the current page will be created if those pages exist. Links will not be generated for pages that do not exist. The current page is also not a link.
Supported options are:
beforeContent to be inserted before the numbers.afterContent to be inserted after the numbers.modelModel to create numbers for, defaults toPaginatorHelper::defaultModel().modulushow many numbers to include on either side of the current page, defaults to 8.firstWhether you want first links generated, set to an integer to define the number of 'first' links to generate. Defaults tofalse. If a string is set a link to the first page will be generated with the value as the title:phpecho $this->Paginator->numbers(['first' => 'First page']);lastWhether you want last links generated, set to an integer to define the number of 'last' links to generate. Defaults tofalse. Follows the same logic as thefirstoption. There is a~PaginatorHelper::last()method to be used separately as well if you wish.
While this method allows a lot of customization for its output. It is also ok to just call the method without any parameters. :
echo $this->Paginator->numbers();Using the first and last options you can create links to the beginning and end of the page set. The following would create a set of page links that include links to the first 2 and last 2 pages in the paged results:
echo $this->Paginator->numbers(['first' => 2, 'last' => 2]);Creating Jump Links
In addition to generating links that go directly to specific page numbers, you'll often want links that go to the previous and next links, first and last pages in the paged data set.
method Cake\View\Helper\PaginatorHelper::prev($title = '<< Previous', $options = [])
method Cake\View\Helper\PaginatorHelper::next($title = 'Next >>', $options = [])
method Cake\View\Helper\PaginatorHelper::first($first = '<< first', $options = [])
method Cake\View\Helper\PaginatorHelper::last($last = 'last >>', $options = [])
Creating Header Link Tags
PaginatorHelper can be used to create pagination link tags in your page <head> elements:
// Create next/prev links for the current model.
echo $this->Paginator->meta();
// Create next/prev & first/last links for the current model.
echo $this->Paginator->meta(['first' => true, 'last' => true]);Added in version 3.4.0
The first and last options were added in 3.4.0
Checking the Pagination State
method Cake\View\Helper\PaginatorHelper::current(string $model = null)
method Cake\View\Helper\PaginatorHelper::hasNext(string $model = null)
method Cake\View\Helper\PaginatorHelper::hasPrev(string $model = null)
method Cake\View\Helper\PaginatorHelper::hasPage(string $model = null, integer $page = 1)
method Cake\View\Helper\PaginatorHelper::total(string $model = null)
Creating a Page Counter
method Cake\View\Helper\PaginatorHelper::counter($options = [])
Returns a counter string for the paged result set. Using a provided format string and a number of options you can create localized and application specific indicators of where a user is in the paged data set.
There are a number of options for counter(). The supported ones are:
formatFormat of the counter. Supported formats are 'range', 'pages' and custom. Defaults to pages which would output like '1 of 10'. In the custom mode the supplied string is parsed and tokens are replaced with actual values. The available tokens are:- the current page displayed.- total number of pages.- current number of records being shown.- the total number of records in the result set.- number of the first record being displayed.- number of the last record being displayed.- The pluralized human form of the model name. If your model was 'RecipePage',would be 'recipe pages'.
You could also supply only a string to the counter method using the tokens available. For example:
phpecho $this->Paginator->counter( 'Page {{page}} of {{pages}}, showing {{current}} records out of {{count}} total, starting on record {{start}}, ending on {{end}}' );Setting 'format' to range would output like '1 - 3 of 13':
phpecho $this->Paginator->counter([ 'format' => 'range' ]);modelThe name of the model being paginated, defaults toPaginatorHelper::defaultModel(). This is used in conjunction with the custom string on 'format' option.
Generating Pagination URLs
method Cake\View\Helper\PaginatorHelper::generateUrl(array $options = [], $model = null, $full = false)
By default returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript). :
echo $this->Paginator->generateUrl(['sort' => 'title']);Creating a Limit Selectbox Control
method Cake\View\Helper\PaginatorHelper::limitControl(array $limits = [], $default = null, array $options = [])
Create a dropdown control that changes the limit query parameter:
// Use the defaults.
echo $this->Paginator->limitControl();
// Define which limit options you want.
echo $this->Paginator->limitControl([25 => 25, 50 => 50]);
// Custom limits and set the selected option
echo $this->Paginator->limitControl([25 => 25, 50 => 50], $user->perPage);The generated form and control will automatically submit on change.
Added in version 3.5.0
The limitControl() method was added in 3.5.0
Configuring Pagination Options
method Cake\View\Helper\PaginatorHelper::options($options = [])
Sets all the options for the PaginatorHelper. Supported options are:
urlThe URL of the paginating action. 'url' has a few sub options as well:sortThe key that the records are sorted by.directionThe direction of the sorting. Defaults to 'ASC'.pageThe page number to display.
The above mentioned options can be used to force particular pages/directions. You can also append additional URL content into all URLs generated in the helper:
php$this->Paginator->options([ 'url' => [ 'sort' => 'email', 'direction' => 'desc', 'page' => 6, 'lang' => 'en' ] ]);The above adds the
enroute parameter to all links the helper will generate. It will also create links with specific sort, direction and page values. By default PaginatorHelper will merge in all of the current passed arguments and query string parameters.escapeDefines if the title field for links should be HTML escaped. Defaults totrue.modelThe name of the model being paginated, defaults toPaginatorHelper::defaultModel().
Example Usage
It's up to you to decide how to show records to the user, but most often this will be done inside HTML tables. The examples below assume a tabular layout, but the PaginatorHelper available in views doesn't always need to be restricted as such.
See the details on PaginatorHelper in the API. As mentioned, the PaginatorHelper also offers sorting features which can be integrated into your table column headers:
<!-- src/Template/Posts/index.ctp -->
<table>
<tr>
<th><?= $this->Paginator->sort('id', 'ID') ?></th>
<th><?= $this->Paginator->sort('title', 'Title') ?></th>
</tr>
<?php foreach ($recipes as $recipe): ?>
<tr>
<td><?= $recipe->id ?> </td>
<td><?= h($recipe->title) ?> </td>
</tr>
<?php endforeach; ?>
</table>The links output from the sort() method of the PaginatorHelper allow users to click on table headers to toggle the sorting of the data by a given field.
It is also possible to sort a column based on associations:
<table>
<tr>
<th><?= $this->Paginator->sort('title', 'Title') ?></th>
<th><?= $this->Paginator->sort('Authors.name', 'Author') ?></th>
</tr>
<?php foreach ($recipes as $recipe): ?>
<tr>
<td><?= h($recipe->title) ?> </td>
<td><?= h($recipe->author->name) ?> </td>
</tr>
<?php endforeach; ?>
</table>NOTE
Sorting by columns in associated models requires setting these in the PaginationComponent::paginate property. Using the example above, the controller handling the pagination would need to set its sortWhitelist key as follows:
$this->paginate = [
'sortWhitelist' => [
'Posts.title',
'Authors.name',
],
];For more information on using the sortWhitelist option, please see Control Which Fields Used For Ordering.
The final ingredient to pagination display in views is the addition of page navigation, also supplied by the PaginationHelper:
// Shows the page numbers
<?= $this->Paginator->numbers() ?>
// Shows the next and previous links
<?= $this->Paginator->prev('« Previous') ?>
<?= $this->Paginator->next('Next »') ?>
// Prints X of Y, where X is current page and Y is number of pages
<?= $this->Paginator->counter() ?>The wording output by the counter() method can also be customized using special markers:
<?= $this->Paginator->counter([
'format' => 'Page {{page}} of {{pages}}, showing {{current}} records out of
{{count}} total, starting on record {{start}}, ending on {{end}}'
]) ?>Paginating Multiple Results
If you are paginating multiple queries you'll need to set the model option when generating pagination related elements. You can either use the model option on every method call you make to PaginatorHelper, or use options() to set the default model:
// Pass the model option
echo $this->Paginator->sort('title', ['model' => 'Articles']);
// Set the default model.
$this->Paginator->options(['model' => 'Articles']);
echo $this->Paginator->sort('title');By using the model option, PaginatorHelper will automatically use the scope defined in when the query was paginated.
Added in version 3.3.0
Multiple Pagination was added in 3.3.0