What is a minimal, complete example of pagination in Kohana 3.2?
-
12-11-2019 - |
Frage
I found much information about pagination in Kohana 3.2 but most of it is scattered across forum comments and blog posts with no single complete source to refer to.
(note: I intended to self answer this question)
Lösung
This is what worked for me:
- Download the pagination module from https://github.com/kloopko/kohana-pagination (pagination was removed from Kohana 3.2, so this is an adapted module).
- Install the module in modules/pagination.
Add the module in bootstrap.php:
Kohana::modules(array( // ... other modules ... 'pagination' => MODPATH.'pagination' ));
Copy the configuration file from modules/pagination/config/pagination.php to application/config/pagination.php.
Add the following actions to your controller:
public function action_index() { // Go to first page by default $this->request->redirect('yourcontroller/page/?page=1'); } public function action_page() { $orm = orm::factory('your_orm'); $pagination = Pagination::factory(array( 'total_items' => $orm->count_all(), 'items_per_page' => 20, ) ); // Pass controller and action names explicitly to $pagination object $pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action())); // Get data $data = $orm->offset($pagination->offset)->limit($pagination->items_per_page)->find_all()->as_array(); // Pass data and validation object to view echo View::factory('yourview/page', array('data' => $data, 'pagination' => $pagination)); }
Create yourview/page as follows:
<?php foreach($data as $item) { // ...put code to list items here } // Show links echo $pagination;
Modify application/config/pagination.php according to your needs. I had to change the
'view'
parameter to'pagination/floating'
which displays ellipses (...) when the list of pages is too large, unlike the default'pagination/basic'
which lists all pages regardless of length.
Andere Tipps
Pagination wasn't originally working/supported in Kohana 3.2. Luckily, somebody has updated the module and you can get the code at https://github.com/kloopko/kohana-pagination