You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CakePHP CMS教程中PaginatorComponent弃用警告的解决方法

CakePHP 分页器组件弃用问题解决方法

问题场景

以下控制器代码会触发分页器组件的弃用警告:

<?php
// src/Controller/ArticlesController.php
namespace App\Controller;

use App\Controller\AppController;

class ArticlesController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();

        $this->loadComponent('Paginator');
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        $articles = $this->Paginator->paginate($this->Articles->find());
        $this->set(compact('articles'));
    }
}

触发的警告信息:

Deprecated (16384) : PaginatorComponent is deprecated, use a Cake\Datasource\Pagination\NumericPaginator instance directly. /home/..../vendor/cakephp/cakephp/src/Controller/ComponentRegistry.php, line: 133

修正方案

移除对Paginator组件的加载,直接调用控制器内置的paginate()方法即可,修正后的代码如下:

<?php
// src/Controller/ArticlesController.php
namespace App\Controller;

use App\Controller\AppController;

class ArticlesController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
        $this->loadComponent('Flash'); // Include the FlashComponent
    }

    public function index()
    {
        // Paginate the ORM table.
        $this->set('articles', $this->paginate($this->Articles));
    }
}

相关内容可参考CakePHP 4.x官方分页文档。

内容的提问来源于stack exchange,提问作者parantur res

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:01:13