CakePHP 2分页给出404地址未找到错误

Ric*_*rst 4 pagination cakephp runtime-error

我正在使用CakePHP 2.3开发一个应用程序,并且遇到了分页问题.当我点击任何分页链接时,我得到一个/ cab/ServiceDirectory/refine/page:2(或哪个曾经链接)在这台服务器上找不到.如果我去/ cab/ServiceDirectory/refine我看到分页链接显示应该有5页.如果我从控制器和视图中删除分页代码,我会看到我应该看到的所有结果.

在我的ServiceDirectoryResultsController中

public function index() {
  $this->ServiceLocation->recursive=0;
}

public function refine ($id=null) (

 // All my code to get the ServiceLocations from the DB

$this->paginate->array(
  'conditions' => array('ServiceLocation.state' => $states[$state], 'ServiceLocation.solution_categories' => $active_cat),
  'limit' => 8,
);

$results = $this->paginate('ServiceLocation');
$this->set('serviceLocation' ,$results);

}
Run Code Online (Sandbox Code Playgroud)

在我看来,我有

<div class="paging">
  <?php
    echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
    echo $this->Paginator->numbers(array('separator' => ''));
    echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
  ?>
</div>

<?php foreach ($serviceLocation as $Location): ?>

  // I echo a few things from the $Location array

<?php endforeach; ?>

<div class="paging">
  <?php
    echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
    echo $this->Paginator->numbers(array('separator' => ''));
    echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
  ?>
</div>
Run Code Online (Sandbox Code Playgroud)

如果没有分页和使用查找全部并将其发送到视图,我会看到我期望的40个结果.

随着分页,我看到分页链接,如果我将鼠标悬停在它们上面,我会看到我的url/cab/ServiceDirectoryResults/refine/page:2或page:3或page:4或page:5

如果我点击其中任何一个链接:pageX就会出现错误

错误:在此服务器上找不到请求的地址'/ cab/ServiceDirectoryResults/refine/page:2'

我已经做了很多关于CakePHP分页的阅读,但找不到任何这种行为的原因.任何人都可以建议一个原因或可能的解决方案或路径来调试此错误?

问候

理查德

我检查了错误级别,它已经​​设置为级别2

这是错误日志中的最后一个条目

2013-05-22 17:09:28 Error: [NotFoundException] Not Found
Request URL: /cab/ServiceDirectoryResults/refine/page:2
Stack Trace:
#0 /var/www/html/cab/lib/Cake/Controller/Controller.php(1074): PaginatorComponent->paginate('ServiceLocation', Array, Array)
#1 /var/www/html/cab/app/Controller/ServiceDirectoryResultsController.php(381): Controller->paginate('ServiceLocation')
#2 [internal function]: ServiceDirectoryResultsController->refine()
#3 /var/www/html/cab/lib/Cake/Controller/Controller.php(486): ReflectionMethod->invokeArgs(Object(ServiceDirectoryResultsController), Array)
#4 /var/www/html/cab/lib/Cake/Routing/Dispatcher.php(187): Controller->invokeAction(Object(CakeRequest))
#5 /var/www/html/cab/lib/Cake/Routing/Dispatcher.php(162): Dispatcher->_invoke(Object(ServiceDirectoryResultsController), Object(CakeRequest), Object(CakeResponse))
#6 /var/www/html/cab/app/webroot/index.php(109): Dispatcher-   >dispatch(Object(CakeRequest), Object(CakeResponse))
#7 {main}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助

小智 6

仅供参考:我正在使用Cakephp 2.4,我只在这个版本上测试过它.

我遇到了同样的问题.我将分页变量存储在用户的会话中.
这样,当用户离开页面时,当他们返回页面时,他们不会松开他们的位置(页面/排序字段/方向).

因为我将变量存储在会话中,然后从会话中检索它们,如果用户点击导致结果小于它们所在页面的链接,我将获得404.

示例(他们所在的页面):
http://localhost/products/index/page:5/sort:Product.name

我发给他们这个链接:
http://localhost/products/index/search:[search_term]

这会将结果过滤为2页.
在这种情况下,恢复会话变量的实际(perma)链接看起来像:
http://localhost/products/index/page:5/sort:Product.name/search:[search_term]

第5页超出了新结果的2页范围.
这将抛出NotFoundException();这里所见:http:
//api.cakephp.org/2.4/source-class-PaginatorComponent.html#238

我如何解决这个问题是为了捕获异常,然后将它们重定向到第一页.由于所有分页变量(包括"搜索")都存储在会话中,我只需要传递页面变量:

例:
http://localhost/products/index/page:1

Cakephp建议将其作为克服它的可行方法:http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#out-of-range-page-requests

但是,我希望这可用于我的所有"索引"页面,所以我覆盖了该Controller::paginate()方法AppController.
这里看到的原始分页方法:http:
//api.cakephp.org/2.4/source-class-Controller.html#1070-1081

以下是我在Controller/AppController.php中的操作方法:

<?php

class CommonAppController extends Controller
{
    // ..... other code
    public function paginate($object = null, $scope = array(), $whitelist = array())
    {
    /*
     * Used to see if we get an exception thrown, 
     * If so, send them to the first page, and set the flash so they know
     */
        try
        {
            // taken from their current paginate();
            // http://api.cakephp.org/2.4/source-class-Controller.html#1070-1081
            return $this->Components->load('Paginator', $this->paginate)->paginate($object, $scope, $whitelist);
        }
        catch (NotFoundException $e)
        {
            //Do something here like redirecting to first or last page.
            //$this->request->params['paging'] will give you required info.
            $page = (isset($this->request->params['named']['page'])?$this->request->params['named']['page']:1);

            $this->Session->setFlash(__('Unable to find results on page: %s. Redirected you to page 1.', $page));
            return $this->redirect(array('page' => 1));
        }
    }
    // ..... other code
}
?>
Run Code Online (Sandbox Code Playgroud)