![]() |
Support for 'perPage' Parameter in Pager Library - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29) +--- Thread: Support for 'perPage' Parameter in Pager Library (/showthread.php?tid=92679) |
Support for 'perPage' Parameter in Pager Library - campio97 - 03-28-2025 Hello, I've been working with the Pager library in CodeIgniter 4 and noticed that while it automatically handles the `page` parameter from the URL query string, it doesn't provide similar functionality for the number of items per page. Current Behavior Currently, the Pager class automatically reads the `page` parameter from the URL (e.g., `?page=5`), but the number of items per page (`perPage`) must be manually specified in the code: PHP Code: // Controller method Proposed Enhancement I'd like to propose adding support for a `perPage` parameter in the URL query string, similar to how the `page` parameter works. This would allow clients to specify how many items they want per page directly in the URL: Code: /users?page=5&perPage=50 The Pager library would automatically detect this parameter and use it instead of the default value or the one specified in the code. Benefits 1. Improved API Development: This would make the Pager more suitable for RESTful API development, where clients often need to control the pagination parameters. 2. Consistent Interface: It would provide a consistent interface for both page number and page size parameters. 3. Reduced Boilerplate: Developers wouldn't need to manually check for and apply the `perPage` parameter in every controller method. 4. Better User Experience: Frontend applications could allow users to choose how many items they want to see per page. Implementation Suggestion The implementation could be similar to how the `page` parameter is handled in the `calculateCurrentPage()` method, with appropriate safety checks to ensure the value is within acceptable limits: PHP Code: // Inside the Pager class This would respect the existing API while adding the new functionality in a backward-compatible way. Has anyone else found this limitation? Would this feature be valuable to the community? Thank you for considering this enhancement! RE: Support for 'perPage' Parameter in Pager Library - michalsn - 03-28-2025 We can simply do PHP Code: $model->paginate($this->request->getGet('perPage')); // previously validating this value IMO this is quite easy to implement when we need this kind of feature, but I'm not against improvements. RE: Support for 'perPage' Parameter in Pager Library - campio97 - 03-28-2025 Yeah but i mentioned it at point 3 of Benefits: 3. Reduced Boilerplate: Developers wouldn't need to manually check for and apply the `perPage` parameter in every controller method. RE: Support for 'perPage' Parameter in Pager Library - michalsn - 03-28-2025 Fair enough. As I said - I'm not against improvements. RE: Support for 'perPage' Parameter in Pager Library - grimpirate - 03-28-2025 You can reduce the boilerplate by creating this functionality in the BaseController if you so desire, you don't have to add it to every controller if you extend from there. RE: Support for 'perPage' Parameter in Pager Library - campio97 - 03-31-2025 I extended the model by creating a new function based on the original paginate method, with the addition of support for the $perPageFromQuery parameter. I also introduced a new property in the model, $perPageMaxLimit, which allows setting a default maximum number of items per page. This makes it possible to define pagination limits on a per-model basis, rather than relying solely on a global setting. PHP Code: /** |