forge($name = 'default', $config = array())
The forge method allows you to create a new pagination instance, and configure it by passing an array.
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | Pagination | |||||||||
Example |
|
The pagination class allows you to easily setup pagination for records you display.
Simple examples on how to use the Pagination class . You can put this inside your action methods in your controller.
$config = array(
'pagination_url' => 'http://localhost/fuel/welcome/index/',
'total_items' => 10,
'per_page' => 5,
'uri_segment' => 3,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
// Create a pagination instance named 'mypagination'
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = DB::select('id', 'value')
->from('pagination')
->limit($pagination->per_page)
->offset($pagination->offset)
->execute()
->as_array();
// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;
// return the view
return \View::forge('welcome/index', $data);
$config = array(
'pagination_url' => 'http://localhost/fuel/posts/index/',
'total_items' => Model_Post::count(),
'per_page' => 10,
'uri_segment' => 3,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = Model_Post::query()
->rows_offset($pagination->offset)
->rows_limit($pagination->per_page)
->get();
// we pass the object, it will be rendered when echo'd in the view
$data['pagination'] = $pagination;
// return the view
return \View::forge('posts/index', $data);
You can configure the pagination instance in several ways. You can pass an array with the configuration when you forge the instance, or you can update the properties directly on the instance.
The following configuration settings can be defined:
Param | Type | Default | Description |
---|---|---|---|
pagination_url | string |
|
The URL of page where you have pagination. If null, Fuel tries to detect it from the current URL. |
uri_segment | integer|string |
|
The URI segment containing the page number (if integer). The query string field containing the page number (if string). |
num_links | integer |
|
The total number of links to show. |
total_items | integer |
|
The total number of items. Usually this is the result of a count() query. |
per_page | integer |
|
The number of items per page. |
current_page | integer |
|
The page to load if no page number is present in the URI. If not given, it defaults to 'default_page'. |
show_first | bool |
|
Generates a 'to the first page' link if true and not already on the first page. |
show_last | bool |
|
Generates a 'to the last page' link if true and not already on the last page. |
link_offset | int / float |
|
Offset of the active link in the pagination block, either in a decimal between 0 and 1, or in an integer (precentage) between 0 and 100. |
default_page | mixed |
|
The page to load if no page number is present in the URI. Valid are 'first', 'last' or a page number. If not given, it defaults to 'first'. |
By default, the Pagination class will try to position the active link (the one for the current page) in the middle of the pagination block.
You can control this behaviour using the link_offset value in the configuration. This value is defined either by a
float between 0 and 1, or by an integer between 0 and 100 (like a percentage). By default this value is set to 0.5
(= 50%).
If you make the value smaller, the active link will move to the left, if you make the value bigger, the active link will move
to the right.
Let's assume you have a total of 20 pages you can display. You have set num_links to 5, displaying links for 5 pages in total, and you're displaying previous and next links as well. The page you have currently selected is page 6.
is displayed, keeping the selected page 6 nicely in the middle of the block.«
4
5
67
8
»
is being displayed, making the selected page 6 the first link in the block.«
67
8
9
10
»
will be displayed.«
2
3
4
5
6»
You can use any value in the range, to progressively shift the active focus either left or right. Obviously, the more links you display, the more granular you can shift the focus.
This only works if there are enough pages to do so. If you only have 5 pages available, you want to display 5 links, and page 3 is the current page, no previous and next links are displayed, and the current page will be in the middle, no matter what the setting of link_offset is. There are simply not enough pages available to move the current page link left or right.
Every pagination instance uses a template to generate the HTML needed to create the pagination markup. You can store your standard templates in the config/pagination.php file. Copy the file from the core config folder to your app config folder before you make any modifications. The default configuration file comes with three templates, the FuelPHP default, and a Twitter Bootstrap v2 and v3 compatible template.
The following template entries must be defined:
wrapper | string |
|
Markup that will wrap the generated pagination markup. |
---|---|---|---|
first | string |
|
Markup that will be used to generate the first page markup. |
first-inactive | string | none | Markup that will be used to generate the first page markup, if this page is first or single. |
first-inactive-link | string | none | Markup that will be used to generate the first page link, if this page is first or single. |
first-marker | string |
|
Markup that will be used to generate the marker of first page. |
first-link | string |
|
Markup that will be used to generate the first page link. |
previous | string |
|
Markup that will be used to generate the previous page markup. |
previous-marker | string |
|
Markup that will be used to generate the marker of previous page. |
previous-link | string |
|
Markup that will be used to generate the previous page link. |
previous-inactive | string |
|
Markup that will be used to generate the previous page markup for inactive links. |
previous-inactive-link | string |
|
Markup that will be used to generate the previous page markup for inactive links. |
regular | string |
|
Markup that will be used to generate the markup for other pages. |
regular-link | string |
|
Markup that will be used to generate the markup for other page links. |
active | string |
|
Markup that will be used to generate the markup for the current page. |
active-link | string |
|
Markup that will be used to generate the markup for the link to the current page. |
next | string |
|
Markup that will be used to generate the next page markup. |
next-marker | string |
|
Markup that will be used to generate the marker of next page. |
next-link | string |
|
Markup that will be used to generate the next page link. |
next-inactive | string |
|
Markup that will be used to generate the next page markup for inactive links. |
next-inactive-link | string |
|
Markup that will be used to generate the next page link for inactive links. |
last | string |
|
Markup that will be used to generate the last page markup. |
last-marker | string |
|
Markup that will be used to generate the marker of last page. |
last-link | string |
|
Markup that will be used to generate the last page link. |
last-inactive | string | none | Markup that will be used to generate the last page markup, if this page is last or single. |
last-inactive-link | string | none | Markup that will be used to generate the last page link, if this page is last or single. |
In the template, {uri} will be replaced by the generated pagination link (by # if inactive template), and {page} by the page number or the previous / next marker. If you want to use an image for these markers, just modify the corresponding link definitions in the template, replacing {page} by the markup for the image.
The configuration you pass when you forge the pagination instance will be merged with the default template defined in your configuration file. This will allow you to only pass the values you want to override. If your template in the configuration file is not complete, the default values as mentioned above will be used.
The forge method allows you to create a new pagination instance, and configure it by passing an array.
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | Pagination | |||||||||
Example |
|
The instance method allows you retrieve a previously forged instance, or return the default instance if no name was given.
Static | Yes | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | mixed. a Pagination object, or false if the requested instance does not exist. | ||||||
Example |
|
The render method generates the markup to display the pagination links in the view.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
The pagination object contains an __toString() method, that will cause the render() method to be called when you echo the object, or cast it to string.
The first method generates the markup to display a "First page" link for pagination. If no string is given to be used as marker, the "first-marker" value from the template will be used.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
The "First page" link will only be shown if there is a first page, and you're not already on it. Note that by default there is no inactive link defined for 'first'.
The previous method generates the markup to display a "Previous" link for pagination. If no string is given to be used as marker, the "previous-marker" value from the template will be used.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
The next method generates the markup to display a "Next" link for pagination. If no string is given to be used as marker, the "next-marker" value from the template will be used.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
The last method generates the markup to display a "Last page" link for pagination. If no string is given to be used as marker, the "last-marker" value from the template will be used.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
The "Last page" link will only be shown if there are any pages, and you're not already on the last one. Note that by default there is no inactive link defined for 'last'.
The pages_render method generates the markup that displays the page links between previous and next links for pagination.
Static | No |
---|---|
Parameters | None |
Returns | mixed |
Example |
|
For your convinience, the Pagination class also has a static interface that operates on the default instance only.
The get method allows you to get a configuration item on the default instance.
Static | Yes | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | mixed, properly value, or null if the property doesn't exist. | ||||||
Example |
|
The set method allows you to set a configuration item on the default instance to the given value.
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | void | |||||||||
Example |
|
For backward compatibility, the static methods set_config(), create_links(), next_link() and prev_link() will be emulated on the default instance, minimizing the migration effort needed when upgrading an existing application to FuelPHP v1.4+.
Note that there is no possible way to emulate direct access to static class properties at the moment, so if your application uses that, you'll have to change them:
// pre v1.4 usage:
Pagination::$per_page = 10;
// new usage:
Pagination::set('per_page', 10);
// pre v1.4 usage:
Model_Article::find()
->order_by('date', 'ASC')
->rows_offset(\Pagination::$offset)
->rows_limit(\Pagination::$per_page)
->get();
// new usage:
Model_Article::query()
->order_by('date', 'ASC')
->rows_offset(\Pagination::get('offset'))
->rows_limit(\Pagination::get('per_page'))
->get();