【问题标题】:How to fetch the Not Found Exception in cakePHP 2 Paginator Component如何在 cakePHP 2 Paginator 组件中获取 Not Found 异常
【发布时间】:2015-11-29 14:00:11
【问题描述】:

我使用 Paginator 组件,每当用户/机器人访问过高的页码时,他都会收到 Not Found 异常。 这可能没问题,但我想将 301 的用户重定向回第一页。

现在我知道 [cakePHP 2 文档][1] 告诉我如何针对个别情况执行此操作,但我想针对所有分页执行此操作,而无需每次都添加代码位。

我尝试在我的应用程序中构建自己的 Paginator 组件,并将核心组件作为父组件,但由于我没有命名空间,所以我只是调用自己的组件并以循环结束

    <?php

    App::uses('PaginatorComponent', 'Vendor/Controller/Component');

    class PaginatorComponent extends PaginatorComponent {

        public function paginate($object = null, $scope = array(), $whitelist = array()) {
            try {
                PaginatorComponent::paginate($object, $scope, $whitelist);
            } catch (NotFoundException $e) {
                if ($this->request->query('page') <= 1) {
                    throw new NotFoundException();
                }
                $this->redirect($this->here);
            }
        }

    }

【问题讨论】:

    标签: php cakephp pagination cakephp-2.3


    【解决方案1】:

    你不能调用你的组件PaginatorComponent,因为它已经存在了!这条线应该立即敲响警钟:-

    class PaginatorComponent extends PaginatorComponent {
    

    而是将您的组件命名为不同的名称,例如 CustomPaginatorComponent:-

    class CustomPaginatorComponent extends PaginatorComponent {
    

    您还需要在自定义的paginate() 方法中调用parent::paginate(),而不是PaginatorComponent::paginate()

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 2021-02-17
      • 2018-10-10
      • 2018-08-22
      • 2017-03-25
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多