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 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
パラメータ |
|
|||||||||
返り値 | Pagination | |||||||||
例 |
|
The pagination class allows you to easily setup pagination for records you display.
A simple example 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,
),
);
// 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();
$data['pagination'] = $pagination->render();
$this->render('welcome/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:
パラメータ | 型 | 規定値 | 説明 |
---|---|---|---|
pagination_url | string | None | The URL of page where you have pagination. |
uri_segment | integer |
|
The URI segment containing the page number. |
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 1. |
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 two templates, the FuelPHP default, and a Twitter Bootstrap v2 compatible template.
The following template entries must be defined:
wrapper | string |
|
Markup that will wrap the generated pagination markup. |
---|---|---|---|
previous | string |
|
Markup that will be used to generate the previous page markup. |
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-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. |
In the template, {uri} will be replaced by the generated pagination link, 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 | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
パラメータ |
|
|||||||||
返り値 | Pagination | |||||||||
例 |
|
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 | None |
返り値 | string |
例 |
|
The next method generates the markup to display a "Next" link for pagination.
Static | No | ||||||
---|---|---|---|---|---|---|---|
パラメータ |
|
||||||
Returns | string | ||||||
Example |
|
The previous method generates the markup to display a "Previous" link for pagination.
Static | No | ||||||
---|---|---|---|---|---|---|---|
Parameters |
|
||||||
Returns | string | ||||||
Example |
|
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 |
例 |
|
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 | ||||||
---|---|---|---|---|---|---|---|
パラメータ |
|
||||||
Returns | mixed, properly value, or null if the property doesn't exist. | ||||||
例 |
|
The set method allows you to set a configuration item on the default instance to the given value.
Static | Yes | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Parameters |
|
|||||||||
Returns | void | |||||||||
例 |
|
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::find()
->order_by('date', 'ASC')
->rows_offset(\Pagination::get('offset'))
->rows_limit(\Pagination::get('per_page'))
->get());