Class PaginatorHelper
Pagination Helper class for easy generation of pagination links.
PaginationHelper encloses all methods needed when working with pagination.
Property Summary
-
$Html public @property
HtmlHelper -
$Number public @property
NumberHelper -
$Url public @property
UrlHelper -
$_View protected
Cake\View\ViewThe View instance this helper is attached to
-
$_config protected
arrayRuntime config
-
$_configInitialized protected
boolWhether the config property has already been configured with defaults
-
$_defaultConfig protected
arrayDefault config for this class
-
$_defaultModel protected
stringDefault model of the paged sets
-
$_helperMap protected
arrayA helper lookup table used to lazy load helper objects.
-
$_templater protected
Cake\View\StringTemplateStringTemplate instance.
-
$fieldset public
arrayHolds the fields ['field_name' => ['type' => 'string', 'length' => 100]], primaryKey and validates ['field_name']
-
$helpers public
arrayList of helpers used by this helper
-
$plugin public
stringPlugin path
-
$request public
Cake\Network\RequestRequest object
-
$tags public
arrayHolds tag templates.
-
$theme public
stringThe current theme name if any.
Method Summary
-
__call() public
Provide non fatal errors on missing method calls.
-
__construct() public
Constructor. Overridden to merge passed args with URL options.
-
__debugInfo() public
Returns an array that can be used to describe the internal state of this object.
-
__get() public
Lazy loads helpers.
-
_configDelete() protected
Delete a single config key
-
_configRead() protected
Read a config variable
-
_configWrite() protected
Write a config variable
-
_confirm() protected
Returns a string to be used as onclick handler for confirm dialogs.
-
_firstNumber() protected
Generates the first number for the paginator numbers() method.
-
_formatNumber() protected
Formats a number for the paginator number output.
-
_getNumbersStartAndEnd() protected
Calculates the start and end for the pagination numbers.
-
_hasPage() protected
Does $model have $page in its range?
-
_lastNumber() protected
Generates the last number for the paginator numbers() method.
-
_modulusNumbers() protected
Generates the numbers for the paginator numbers() method.
-
_numbers() protected
Generates the numbers for the paginator numbers() method.
-
_toggledLink() protected
Generate an active/inactive link for next/prev methods.
-
addClass() public
Adds the given class to the element options
-
config() public
Usage
-
configShallow() public
Merge provided config with existing config. Unlike
config()which does a recursive merge for nested keys, this method does a simple merge. -
counter() public
Returns a counter string for the paged result set
-
current() public
Gets the current page of the recordset for the given model
-
defaultModel() public
Gets the default model of the paged sets
-
first() public
Returns a first or set of numbers for the first pages.
-
formatTemplate() public
Format a template string with $data
-
generateUrl() public
Merges passed URL options with current pagination state to generate a pagination URL.
-
hasNext() public
Returns true if the given result set is not at the last page
-
hasPage() public
Returns true if the given result set has the page number given by $page
-
hasPrev() public
Returns true if the given result set is not at the first page
-
implementedEvents() public
Event listeners.
-
last() public
Returns a last or set of numbers for the last pages.
-
meta() public
Returns the meta-links for a paginated result set.
-
next() public
Generates a "next" link for a set of paged records
-
numbers() public
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 (default: 8).
-
options() public
Sets default options for all pagination links
-
param() public
Convenience access to any of the paginator params.
-
params() public
Gets the current paging parameters from the resultset for the given model
-
prev() public
Generates a "previous" link for a set of paged records
-
sort() public
Generates a sorting link. Sets named parameters for the sort and direction. Handles direction switching automatically.
-
sortDir() public
Gets the current direction the recordset is sorted
-
sortKey() public
Gets the current key by which the recordset is sorted
-
templater() public
templater
-
templates() public
Get/set templates to use.
Method Detail
__call() ¶ public
__call(string $method, array $params): void
Provide non fatal errors on missing method calls.
Parameters
-
string$method Method to invoke
-
array$params Array of params for the method.
Returns
void__construct() ¶ public
__construct(Cake\View\View $View, array $config = [])
Constructor. Overridden to merge passed args with URL options.
Parameters
-
Cake\View\View$View The View this helper is being attached to.
-
array$config optional Configuration settings for the helper.
__debugInfo() ¶ public
__debugInfo(): array
Returns an array that can be used to describe the internal state of this object.
Returns
array__get() ¶ public
__get(string $name): Cake\View\Helper|void
Lazy loads helpers.
Parameters
-
string$name Name of the property being accessed.
Returns
Cake\View\Helper|void_configDelete() ¶ protected
_configDelete(string $key): void
Delete a single config key
Parameters
-
string$key Key to delete.
Returns
voidThrows
Cake\Core\Exception\Exceptionif attempting to clobber existing config
_configRead() ¶ protected
_configRead(string|null $key): mixed
Read a config variable
Parameters
-
string|null$key Key to read.
Returns
mixed_configWrite() ¶ protected
_configWrite(string|array $key, mixed $value, bool|string $merge = false): void
Write a config variable
Parameters
-
string|array$key Key to write to.
-
mixed$value Value to write.
-
bool|string$merge optional True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Returns
voidThrows
Cake\Core\Exception\Exceptionif attempting to clobber existing config
_confirm() ¶ protected
_confirm(string $message, string $okCode, string $cancelCode = '', array $options = []): string
Returns a string to be used as onclick handler for confirm dialogs.
Parameters
-
string$message Message to be displayed
-
string$okCode Code to be executed after user chose 'OK'
-
string$cancelCode optional Code to be executed after user chose 'Cancel'
-
array$options optional Array of options
Returns
string_firstNumber() ¶ protected
_firstNumber(Cake\View\StringTemplate $ellipsis, array $params, int $start, array $options): string
Generates the first number for the paginator numbers() method.
Parameters
-
Cake\View\StringTemplate$ellipsis StringTemplate instance.
-
array$params Params from the numbers() method.
-
int$start Start number.
-
array$options Options from the numbers() method.
Returns
string_formatNumber() ¶ protected
_formatNumber(Cake\View\StringTemplate $templater, array $options): string
Formats a number for the paginator number output.
Parameters
-
Cake\View\StringTemplate$templater StringTemplate instance.
-
array$options Options from the numbers() method.
Returns
string_getNumbersStartAndEnd() ¶ protected
_getNumbersStartAndEnd(array $params, array $options): array
Calculates the start and end for the pagination numbers.
Parameters
-
array$params Params from the numbers() method.
-
array$options Options from the numbers() method.
Returns
array_hasPage() ¶ protected
_hasPage(string $model, int $page): bool
Does $model have $page in its range?
Parameters
-
string$model Model name to get parameters for.
-
int$page Page number you are checking.
Returns
bool_lastNumber() ¶ protected
_lastNumber(Cake\View\StringTemplate $ellipsis, array $params, int $end, array $options): string
Generates the last number for the paginator numbers() method.
Parameters
-
Cake\View\StringTemplate$ellipsis StringTemplate instance.
-
array$params Params from the numbers() method.
-
int$end End number.
-
array$options Options from the numbers() method.
Returns
string_modulusNumbers() ¶ protected
_modulusNumbers(Cake\View\StringTemplate $templater, array $params, array $options): string
Generates the numbers for the paginator numbers() method.
Parameters
-
Cake\View\StringTemplate$templater StringTemplate instance.
-
array$params Params from the numbers() method.
-
array$options Options from the numbers() method.
Returns
string_numbers() ¶ protected
_numbers(Cake\View\StringTemplate $templater, array $params, array $options): string
Generates the numbers for the paginator numbers() method.
Parameters
-
Cake\View\StringTemplate$templater StringTemplate instance.
-
array$params Params from the numbers() method.
-
array$options Options from the numbers() method.
Returns
string_toggledLink() ¶ protected
_toggledLink(string|bool $text, bool $enabled, array $options, array $templates): string
Generate an active/inactive link for next/prev methods.
Parameters
-
string|bool$text The enabled text for the link.
-
bool$enabled Whether or not the enabled/disabled version should be created.
-
array$options An array of options from the calling method.
-
array$templates An array of templates with the 'active' and 'disabled' keys.
Returns
stringaddClass() ¶ public
addClass(array $options = [], string $class = null, string $key = 'class'): array
Adds the given class to the element options
Parameters
-
array$options optional Array options/attributes to add a class to
-
string$class optional The class name being added.
-
string$key optional the key to use for class.
Returns
arrayconfig() ¶ public
config(string|array|null $key = null, mixed|null $value = null, bool $merge = true): mixed
Usage
Reading the whole config:
$this->config();
Reading a specific value:
$this->config('key');
Reading a nested value:
$this->config('some.nested.key');
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
-
string|array|null$key optional The key to get/set, or a complete array of configs.
-
mixed|null$value optional The value to set.
-
bool$merge optional Whether to recursively merge or overwrite existing config, defaults to true.
Returns
mixedThrows
Cake\Core\Exception\ExceptionWhen trying to set a key that is invalid.
configShallow() ¶ public
configShallow(string|array $key, mixed|null $value = null): $this
Merge provided config with existing config. Unlike config() which does
a recursive merge for nested keys, this method does a simple merge.
Setting a specific value:
$this->config('key', $value);
Setting a nested value:
$this->config('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->config(['one' => 'value', 'another' => 'value']);
Parameters
-
string|array$key The key to set, or a complete array of configs.
-
mixed|null$value optional The value to set.
Returns
$thiscounter() ¶ public
counter(string|array $options = []): string
Returns a counter string for the paged result set
Options
modelThe model to use, defaults to PaginatorHelper::defaultModel();formatThe format string you want to use, defaults to 'pages' Which generates output like '1 of 5' set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing the following placeholders{{page}},{{pages}},{{current}},{{count}},{{model}},{{start}},{{end}}and any custom content you would like.
Parameters
-
string|array$options optional Options for the counter string. See #options for list of keys. If string it will be used as format.
Returns
stringLinks
current() ¶ public
current(string|null $model = null): int
Gets the current page of the recordset for the given model
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
Returns
intLinks
defaultModel() ¶ public
defaultModel(): string|null
Gets the default model of the paged sets
Returns
string|nullfirst() ¶ public
first(string|int $first = '<< first', array $options = []): string
Returns a first or set of numbers for the first pages.
echo $this->Paginator->first('< first');
Creates a single link for the first page. Will output nothing if you are on the first page.
echo $this->Paginator->first(3);
Will create links for the first 3 pages, once you get to the third or greater page. Prior to that nothing will be output.
Options:
modelThe model to use defaults to PaginatorHelper::defaultModel()escapeWhether or not to HTML escape the text.urlAn array of additional URL options to use for link generation.
Parameters
-
string|int$first optional if string use as label for the link. If numeric, the number of page links you want at the beginning of the range.
-
array$options optional An array of options.
Returns
stringLinks
formatTemplate() ¶ public
formatTemplate(string $name, array $data): string
Format a template string with $data
Parameters
-
string$name The template name.
-
array$data The data to insert.
Returns
stringgenerateUrl() ¶ public
generateUrl(array $options = [], string|null $model = null, bool $full = false): mixed
Merges passed URL options with current pagination state to generate a pagination URL.
Parameters
-
array$options optional Pagination/URL options array
-
string|null$model optional Which model to paginate on
-
bool$full optional If true, the full base URL will be prepended to the result
Returns
mixedLinks
hasNext() ¶ public
hasNext(string|null $model = null): bool
Returns true if the given result set is not at the last page
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
Returns
boolLinks
hasPage() ¶ public
hasPage(string $model = null, int $page = 1): bool
Returns true if the given result set has the page number given by $page
Parameters
-
string$model optional Optional model name. Uses the default if none is specified.
-
int$page optional The page number - if not set defaults to 1.
Returns
boolLinks
hasPrev() ¶ public
hasPrev(string|null $model = null): bool
Returns true if the given result set is not at the first page
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
Returns
boolLinks
implementedEvents() ¶ public
implementedEvents(): array
Event listeners.
By defining one of the callback methods a helper is assumed to be interested in the related event.
Override this method if you need to add non-conventional event listeners. Or if you want helpers to listen to non-standard events.
Returns
arraylast() ¶ public
last(string|int $last = 'last >>', array $options = []): string
Returns a last or set of numbers for the last pages.
echo $this->Paginator->last('last >');
Creates a single link for the last page. Will output nothing if you are on the last page.
echo $this->Paginator->last(3);
Will create links for the last 3 pages. Once you enter the page range, no output will be created.
Options:
modelThe model to use defaults to PaginatorHelper::defaultModel()escapeWhether or not to HTML escape the text.urlAn array of additional URL options to use for link generation.
Parameters
-
string|int$last optional if string use as label for the link, if numeric print page numbers
-
array$options optional Array of options
Returns
stringLinks
meta() ¶ public
meta(array $options = []): string|null
Returns the meta-links for a paginated result set.
echo $this->Paginator->meta();
Echos the links directly, will output nothing if there is neither a previous nor next page.
$this->Paginator->meta(['block' => true]);
Will append the output of the meta function to the named block - if true is passed the "meta" block is used.
Options:
modelThe model to use defaults to PaginatorHelper::defaultModel()blockThe block name to append the output to, or false/absenst to return as a string
Parameters
-
array$options optional Array of options
Returns
string|nullnext() ¶ public
next(string $title = 'Next >>', array $options = []): string
Generates a "next" link for a set of paged records
Options:
disabledTitleThe text to used when the link is disabled. This defaults to the same text at the active link. Setting to false will cause this method to return ''.escapeWhether you want the contents html entity encoded, defaults to truemodelThe model to use, defaults to PaginatorHelper::defaultModel()urlAn array of additional URL options to use for link generation.templatesAn array of templates, or template file name containing the templates you'd like to use when generating the link for next page. The helper's original templates will be restored once next() is done.
Parameters
-
string$title optional Title for the link. Defaults to 'Next >>'.
-
array$options optional Options for pagination link. See above for list of keys.
Returns
stringLinks
numbers() ¶ public
numbers(array $options = []): string
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 (default: 8).
$this->Paginator->numbers(['first' => 2, 'last' => 2]);
Using the first and last options you can create links to the beginning and end of the page set.
Options
beforeContent to be inserted before the numbers, but after the first links.afterContent to be inserted after the numbers, but before the last links.modelModel to create numbers for, defaults to PaginatorHelper::defaultModel()modulusHow many numbers to include on either side of the current page, defaults to 8. Set tofalseto disable and to show all numbers.firstWhether you want first links generated, set to an integer to define the number of 'first' links to generate.lastWhether you want last links generated, set to an integer to define the number of 'last' links to generate.templatesAn array of templates, or template file name containing the templates you'd like to use when generating the numbers. The helper's original templates will be restored once numbers() is done.urlAn array of additional URL options to use for link generation.
The generated number links will include the 'ellipsis' template when the first and last options
and the number of pages exceed the modulus. For example if you have 25 pages, and use the first/last
options and a modulus of 8, ellipsis content will be inserted after the first and last link sets.
Parameters
-
array$options optional Options for the numbers.
Returns
stringLinks
options() ¶ public
options(array $options = []): void
Sets default options for all pagination links
Parameters
-
array$options optional Default options for pagination links. See PaginatorHelper::$options for list of keys.
Returns
voidparam() ¶ public
param(string $key, string|null $model = null): mixed
Convenience access to any of the paginator params.
Parameters
-
string$key Key of the paginator params array to retrieve.
-
string|null$model optional Optional model name. Uses the default if none is specified.
Returns
mixedparams() ¶ public
params(string|null $model = null): array
Gets the current paging parameters from the resultset for the given model
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
Returns
arrayprev() ¶ public
prev(string $title = '<< Previous', array $options = []): string
Generates a "previous" link for a set of paged records
Options:
disabledTitleThe text to used when the link is disabled. This defaults to the same text at the active link. Setting to false will cause this method to return ''.escapeWhether you want the contents html entity encoded, defaults to truemodelThe model to use, defaults to PaginatorHelper::defaultModel()urlAn array of additional URL options to use for link generation.templatesAn array of templates, or template file name containing the templates you'd like to use when generating the link for previous page. The helper's original templates will be restored once prev() is done.
Parameters
-
string$title optional Title for the link. Defaults to '<< Previous'.
-
array$options optional Options for pagination link. See above for list of keys.
Returns
stringLinks
sort() ¶ public
sort(string $key, string|null $title = null, array $options = []): string
Generates a sorting link. Sets named parameters for the sort and direction. Handles direction switching automatically.
Options:
escapeWhether you want the contents html entity encoded, defaults to true.modelThe model to use, defaults to PaginatorHelper::defaultModel().directionThe default direction to use when this link isn't active.lockLock direction. Will only use the default direction then, defaults to false.
Parameters
-
string$key The name of the key that the recordset should be sorted.
-
string|null$title optional Title for the link. If $title is null $key will be used for the title and will be generated by inflection.
-
array$options optional Options for sorting link. See above for list of keys.
Returns
stringLinks
sortDir() ¶ public
sortDir(string|null $model = null, array $options = []): string
Gets the current direction the recordset is sorted
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
-
array$options optional Options for pagination links. See #options for list of keys.
Returns
stringLinks
sortKey() ¶ public
sortKey(string|null $model = null, array $options = []): string|null
Gets the current key by which the recordset is sorted
Parameters
-
string|null$model optional Optional model name. Uses the default if none is specified.
-
array$options optional Options for pagination links. See #options for list of keys.
Returns
string|nullLinks
templater() ¶ public
templater(): Cake\View\StringTemplate
templater
Returns
Cake\View\StringTemplatetemplates() ¶ public
templates(string|null|array $templates = null): $this|string|array
Get/set templates to use.
Parameters
-
string|null|array$templates optional null or string allow reading templates. An array allows templates to be added.
Returns
$this|string|arrayProperty Detail
$_configInitialized ¶ protected
Whether the config property has already been configured with defaults
Type
bool$_defaultConfig ¶ protected
Default config for this class
Options: Holds the default options for pagination links
The values that may be specified are:
urlUrl of the action. See Router::url()url['sort']the key that the recordset is sorted.url['direction']Direction of the sorting (default: 'asc').url['page']Page number to use in links.modelThe name of the model.escapeDefines if the title field for the link should be escaped (default: true).
Templates: the templates used by this class
Type
array$fieldset ¶ public
Holds the fields ['field_name' => ['type' => 'string', 'length' => 100]], primaryKey and validates ['field_name']
Type
array