不能直接 new Pagerfanta(),因CI3无Composer自动加载、CI4默认未配置Pagerfanta的PSR-4命名空间;需手动封装代理类(CI3)或在Autoload.php中添加'Pagerfanta' => ROOTPATH.'vendor/pagerfanta/pagerfanta/src/'(CI4)。

Pagerfanta 本身不原生支持 CodeIgniter,直接集成会报 Class 'PagerfantaPagerfanta' not found 或路由/自动加载失败——根本原因是 CodeIgniter 3/4 的类加载机制与 Pagerfanta 的 PSR-4 结构不兼容,不是“加几行代码就能用”。
为什么不能直接 new Pagerfanta()?
CodeIgniter 3 没有 Composer 自动加载(除非手动配置),CI4 虽支持但默认不加载 vendor 下非框架类;而 Pagerfanta 依赖 PagerfantaAdapterArrayAdapter、PagerfantaViewTwitterBootstrap3View 等完整命名空间链。直接 new Pagerfanta(...) 会因类未声明或路径错误中断。
- CI3 中需在
application/libraries/手动封装一层代理类,并 require 相关 Pagerfanta 文件 - CI4 中必须确保
composer.json已运行composer require pagerfanta/pagerfanta,且$autoload->psr4已包含Pagerfanta\命名空间(默认不含) - CI4 的
Services::pager()和 Pagerfanta 冲突:两者都叫 “pager”,但实现完全不同,混用会导致Pager实例被覆盖
CI4 中正确加载 Pagerfanta 类的最小配置
在 app/Config/Autoload.php 的 $psr4 数组里补上:
$psr4 = [
'App\' => APPPATH,
'Pagerfanta\' => ROOTPATH . 'vendor/pagerfanta/pagerfanta/src/',
];
然后控制器中可安全使用:
use PagerfantaPagerfanta;
use PagerfantaAdapterArrayAdapter;
use PagerfantaViewTwitterBootstrap5View;
$data = $this->model->findAll(); // 假设是全量数据(小数据集)
$adapter = new ArrayAdapter($data);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(10);
$pagerfanta->setCurrentPage($this->request->getGet('page', FILTER_SANITIZE_NUMBER_INT) ?: 1);
// 注意:不要用 CI4 的 $pager = Services::pager(),这里用纯 Pagerfanta 实例
$dataForView = $pagerfanta->getCurrentPageResults();
CI3 中绕过自动加载的手动封装方案
在 application/libraries/Pagerfanta_lib.php 中写:
<?php
require_once APPPATH.'third_party/pagerfanta/src/Adapter/ArrayAdapter.php';
require_once APPPATH.'third_party/pagerfanta/src/Adapter/DoctrineORMAdapter.php';
require_once APPPATH.'third_party/pagerfanta/src/View/TwitterBootstrap3View.php';
require_once APPPATH.'third_party/pagerfanta/src/Exception/NotValidCurrentPageException.php';
require_once APPPATH.'third_party/pagerfanta/src/Exception/NotValidMaxPerPageException.php';
require_once APPPATH.'third_party/pagerfanta/src/Pagerfanta.php';
class Pagerfanta_lib {
public $instance;
public function __construct($config = []) {
$this->instance = new PagerfantaPagerfanta(
new PagerfantaAdapterArrayAdapter($config['data'] ?? [])
);
$this->instance->setMaxPerPage($config['per_page'] ?? 10);
$this->instance->setCurrentPage($config['page'] ?? 1);
}
}
调用时:
$this->load->library('pagerfanta_lib', [
'data' => $query_result,
'per_page' => 15,
'page' => $this->uri->segment(3)
]);
$items = $this->pagerfanta_lib->instance->getCurrentPageResults();
$rendered = $this->pagerfanta_lib->instance
->getPaginationTemplate(new PagerfantaViewTwitterBootstrap3View());
分页 HTML 渲染时容易漏掉的关键点
Pagerfanta 默认生成的 HTML 不带 base URL,CI 的分页链接会丢失 index.php 或子目录前缀,导致点击 404:
- CI4 中需显式传入
route参数:$view->render($pagerfanta, ['route' => 'posts/index']),否则用getCurrentUrl()会丢 segment - CI3 中必须重写
generateRoute方法,或在视图里用base_url("controller/method/{$page}")手动拼接 - Bootstrap 视图类版本要匹配项目 CSS:用
TwitterBootstrap5View却引入 BS4 的 CSS,翻页按钮会错位 -
ArrayAdapter只适合小数据集;真实场景务必换成DoctrineORMAdapter或自定义数据库适配器,否则findAll()把十万条记录全查出来再切片,内存直接炸
Pagerfanta 的核心价值在适配器抽象层,而不是渲染模板——别只盯着怎么让数字显示出来,先确认你用的是哪个 Adapter、数据是不是真分页取的。


















