2010-11-19 53 views
0

我是新來CakePHP的...我有一個網頁的URL這樣的:更改CakePhp1.3 paginator目標網址?

http://localhost/books/filteredByAuthor/John-Doe 

所以控制器'books',動作'filteredByAuthor'和'John-Doe'是參數..但是URL看起來很醜,所以我已經添加了一個路線是這樣的:

Router::connect('/author/:name', array('controller' => 'books','action' => 'filteredByAuthor'), array('pass'=>array('name'),'name'=>".*")); 

,現在我的鏈接是:

http://localhost/author/John-Doe 

問題是,認爲有一個分頁程序,當我改變頁面(通過點擊下一個或上一個按鈕)。該分頁程序不會考慮我的路由...並會將該網址更改爲這個

http://localhost/books/filteredByAuthor/John-Doe/page:2 

在我看來,代碼只是:

<?php echo $this->Paginator->prev('<< ' . __('previous', true), array(), null, array('class'=>'disabled'));?> 

該文檔沒有說任何關於避免這一點,我花了幾個小時閱讀paginators源代碼和API ..最後我只是想我的鏈接是這樣的東西:(包括排序和方向在網址上)

http://localhost/author/John-Doe/1/name/asc 

有沒有可能做到這一點,怎麼做?

回答

1

不想回答我的問題......但是這可能一段時間保存到另一個developper =)(是所有關於獲得好人緣)

我發現,你可以傳遞一個「選項」陣列的paginator,並在該數組內,您可以指定分頁程序將用於創建鏈接的url(控制器,操作和參數的數組)。因此,您必須在routes.php文件中編寫所有可能的路由。基本上有3種可能性:

  • 當「頁」沒有定義

例如:

http://localhost/author/John-Doe 

的分頁程序將認爲它的第一頁。相應的路徑將是:

Router::connect('/author/:name', array('controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name'),'name'=>'[a-zA-Z\-]+')); 
  • 當 「頁」 被定義

例如:

http://localhost/author/John-Doe/3 (page 3) 

路線將是:

Router::connect('/author/:name/:page', array('controller' => 'books','action' => 'filteredByAuthor'),array('pass'=>array('name','page'),'name'=>'[a-zA-Z\-]+','page'=>'[0-9]+')); 
  • 最後當頁面和排序在url上定義時(通過點擊由paginator創建的sort鏈接)。

例如:

http://localhost/author/John-Doe/3/title/desc (John Doe's books ordered desc by title) 

的路線是:

Router::connect('/author/:name/:page/:sort/:direction', array('controller' => 'books','action' => 'filteredByAuthor'), 
      array('pass'=>array('name','page','sort','direction'), 
      'name'=>"[a-zA-Z\-]+", 
      'page'=>'[0-9]*', 
      'sort'=>'[a-zA-Z\.]+', 
      'direction'=>'[a-z]+', 
      )); 

的觀點,你必須取消設置由分頁程序創建的URL,因爲你會指定自己的URL陣列在控制器上:

控制器:

function filteredByAuthor($name = null,$page = null , $sort = null , $direction = null){ 
$option_url = array('controller'=>'books','action'=>'filteredByAuthor','name'=>$name); 
if($sort){ 
    $this->passedArgs['sort'] = $sort; 
    $options_url['sort'] = $sort; 
} 
if($direction){ 
    $this->passedArgs['direction'] = $direction; 
    $options_url['direction'] = $direction; 
} 

使用set()...所以在視圖中你需要這樣做送出可變$options_url的觀點:

查看:

unset($this->Paginator->options['url']); 
echo $this->Paginator->prev(__('« Précédente', true), array('url'=>$options_url), null, array('class'=>'disabled')); 
echo $this->Paginator->numbers(array('separator'=>'','url'=>$options_url)); 
echo $this->Paginator->next(__('Suivante »', true), array('url'=>$options_url), null, array('class' => 'disabled')); 

現在,排序鏈接您需要取消設置變量'排序'和'方向'。我們已經使用了他們創造的分頁程序的鏈接,但如果我們不將其刪除,那麼sort()函數將使用它們......我們將無法進行排序=)

$options_sort = $options_url; 
unset($options_sort['direction']); 
unset($options_sort['sort']); 
echo $this->Paginator->sort('Produit <span>&nbsp;</span>', 'title',array('escape'=>false,'url'=>$options_sort)); 

希望這有助於=)