Pagination Paginator(从Cake 1.3迁移到2.0)

Pagination Paginator(从Cake 1.3迁移到2.0),pagination,migration,cakephp-2.0,Pagination,Migration,Cakephp 2.0,我正在与CakePHP2.0中的paginator进行斗争。当我尝试将我的应用程序迁移到2.0时,我找不到任何直接跳到最后一页的解决方案。在1.3中,从外面这样做很安静: echo $this->Html->link(__('Flights'), array('controller' => 'flights', 'action' => 'index','page' => 'last')); 但是,在2.0中,这种将“页面:最后一个”放入的小技巧不再有效。当

我正在与CakePHP2.0中的paginator进行斗争。当我尝试将我的应用程序迁移到2.0时,我找不到任何直接跳到最后一页的解决方案。在1.3中,从外面这样做很安静:

echo $this->Html->link(__('Flights'), array('controller' => 'flights',
    'action' => 'index','page' => 'last'));

但是,在2.0中,这种将“页面:最后一个”放入的小技巧不再有效。当然有一个名为last的Paginator函数,但这只有在我已经在应用程序中时才有帮助。我的问题是直接从外部链接访问分页器的最后一页。

这是一种简单的方法:

echo  $this->Paginator->last('Any text');
获取最后一页编号的其他方法是:

echo  $this->Paginator->counter(array('format' => '{:pages}'));
然后你可以用它来生成你的链接

有关更多信息:

在为这个问题创建悬赏后不久,我使用CakePHP 2.2.4找到了问题的解决方案。我试图完成同样的任务,但使用版本2.2.4而不是2.0。基本上,如果我有一个链接,看起来像控制器的分页方法会知道该转到哪个页面,并显示该页面的正确结果文章。例如,如果我有110篇文章,且分页限制设置为25,则转到该URL将显示第5页(共5页),显示记录101-110。如果我转到“page:first”,我也希望有相同的功能

我需要更改我的库文件lib/Cake/Controller/Component/PaginatorComponent.php

我变了

if (intval($page) < 1) {
    $page = 1;
}
排队后

$pageCount = intval(ceil($count / $limit));
Christian Waschke,有了这个解决方案,你可以使用同样的链接助手,就像你在问题中写的那样。对我来说,链接帮助器是这样的

<?php echo $this->Html->link('Go to Last Page', array('controller' => 'articles', 'action' => 'index', 'page' => 'last')); ?>

如果“last”作为页码传递,您可以自己“计算”最后一页

我不鼓励对CakePHP库文件进行修改,因为这将使将来很难执行升级

基本上,PaginatorHelper使用由PaginatorComponent计算和设置的视图变量,如下所示:

你可以在行动中复制这一点;例如:

public function index()
{
    if (!empty($this->request->params['named']['page'])) {
        switch($this->request->params['named']['page']) {
           case 'first':
                // replace the 'last' with actual number of the first page
                $this->request->params['named']['page'] = 1;
                break;

           case 'last':
                // calculate the last page
                $limit = 10; // your limit here
                $count = $this->Flight->find('count');
                $pageCount = intval(ceil($count / $limit));

                // replace the 'last' with actual number of the last page
                $this->request->params['named']['page'] = $pageCount;
                break;
        }

    }

    // then, paginate as usual
    $this->set('data', $this->paginate('Flight'));
}
为了改进这一点,应该将此逻辑移动到单独的方法或行为。然而如上所述,不需要在PaginatorComponent中进行修改

还请注意,我的示例中的“findcount”没有附加条件,如果需要,应该添加这些条件


如果您查看paginate的CakePHP1.3源代码,上面的代码是可比较的

对不起,我想这是个误会。我想访问如下URL:。这在1.3完美版中有效。对不起,也许是因为我的例子,我想这很难理解。但非常感谢您的阅读和回答。我仍然希望在CakePHP2.0中找到解决方案。我对同样的问题感兴趣,但我不想添加新问题。有人帮忙吗。
<?php echo $this->Html->link('Go to Last Page', array('controller' => 'articles', 'action' => 'index', 'page' => 'last')); ?>
public function index()
{
    if (!empty($this->request->params['named']['page'])) {
        switch($this->request->params['named']['page']) {
           case 'first':
                // replace the 'last' with actual number of the first page
                $this->request->params['named']['page'] = 1;
                break;

           case 'last':
                // calculate the last page
                $limit = 10; // your limit here
                $count = $this->Flight->find('count');
                $pageCount = intval(ceil($count / $limit));

                // replace the 'last' with actual number of the last page
                $this->request->params['named']['page'] = $pageCount;
                break;
        }

    }

    // then, paginate as usual
    $this->set('data', $this->paginate('Flight'));
}