2013-11-28 50 views
0

我在CakePHP中,初學者,我的版本是2.4.3超出範圍頁面的請求

在文件我在下面找到

public function index() { 
    try { 
     $this->Paginator->paginate(); 
    } catch (NotFoundException $e) { 
     //Do something here like redirecting to first or last page. 
     //$this->request->params['paging'] will give you required info. 
    } 
} 

我的問題示例代碼:

1.How到重定向到最後一頁,有什麼辦法可以獲得總頁數?

2.我試圖用debug()輸出$ this-> request-> params ['paging'],但沒有顯示,只是一個空值,我做錯了什麼?

請幫幫我,認爲不是可用

回答

0

paging數據是一個錯誤,或者文件是錯誤的 - 我會說是前者,因爲這將是有點多餘的,當重新計數記錄Paginator組件已經做到了。

望着負責代碼:

https://github.com/cakephp/cakephp/blob/2.4.3/lib/Cake/Controller/Component/PaginatorComponent.php#L215

顯示,前pagingparams陣列中,在設定的異常被拋出。所以直到這個是固定的,你要麼必須修改核心,或計數和對自己的重新計算,這樣的事情(可能需要一些調整):

public function index() 
{ 
    try 
    { 
     $this->Paginator->paginate(); 
    } 
    catch(NotFoundException $e) 
    { 
     extract($this->Paginator->settings); 
     $count = $this->ModelName->find('count', compact('conditions')); 
     $pageCount = intval(ceil($count/$limit)); 
     $this->redirect(array('page' => $pageCount)); 
    } 
} 
+0

Got it!非常感謝你!! – Daruma

1

介紹CakePHP 3.x中,它適用於我。我希望幫助你。

try { 
     $this->paginate(); 
    } 
catch (NotFoundException $e) { 
     $page = $this->request->params['?']['page'] - 1; 
     return $this->redirect(['action' => 'index', 'page' => $page]); 
    }