2010-10-21 21 views
1

我有一個名爲TagsController的控制器,它從index操作中的url中獲取標籤名稱以獲取具有該標籤的項目列表。CakePHP:將'tags/index/php'鏈接改爲'tags/php'

<?php 
foreach($tags as $tag){ 
    echo "<span class='homepagetags'>".$html->link($tag['t']['tag'], array('controller' => 'tags', $tag['t']['tag'])) . "</span> x " . $tag[0]['NumOccurrances'] . "<br><br>"; 
} 
?> 

鏈接帶我去'標籤/索引/ PHP的時候我真的希望它是「標籤/ PHP的

這是一個路由解決方案?

回答

3

具體來說,您需要:

// routes.php 
Router::connect(
    '/tags/:tag', 
    array('controller' => 'tags', 'action' => 'index') 
); 

然後創建一個鏈接:

echo $html->link(
    'PHP Tag', 
    array('controller' => 'tags', 'action' => 'index', 'tag' => 'php') 
); 
+0

這是輝煌的,但現在唯一的問題是我的標籤控制器是給我和錯誤與功能索引($標籤){ \t \t \t \t}錯誤是'缺少參數1 TagsController :: index() '當我從行動中刪除參數錯誤消失 – iamjonesy 2010-10-22 06:40:10

+0

回答我的問題上面:\t $ tag = mysql_real_escape_string($ this-> params ['tag']); – iamjonesy 2010-10-22 13:28:10

+0

很高興你知道了! :) – Till 2010-10-23 08:54:01

2

是的,有一個路由解決方案。它是在食譜的Defining Routes部分中解釋的。這個例子是:

Router::connect(
    '/:controller/:id', 
    array('action' => 'view'), 
    array('id' => '[0-9]+') 
);