2009-11-12 99 views

回答

6

當然。唯一的要求是URL中有足夠的唯一信息來鎖定您想要的文章。如果/article-name在數據庫中是唯一的,則可以使用它來查找所需的特定記錄。

在配置/ routes.php文件:

// ... configure all normal routes first ... 

Router::connect('/*', array('controller' => 'articles', 'action' => 'view')); 

在控制器/ articles_controller.php:

function view ($article_name) { 
    $article = $this->Article->find('first', array(
     'conditions' => array('Article.name' => $article_name) 
    )); 
    ... 
} 

要小心,不要命名您的產品,如任何可以合法地出現在URL,這樣你不會遇到衝突。網址http://example.com/pages是指向產品'網頁'還是指向array('controller' => 'pages', 'action' => 'index')?爲此,您還需要在routes.php中定義您的路線,以便您可以首先訪問所有控制器,並且只有未定義的休息纔會被傳送到您的ArticlesController。查看third parameter of Routes::connect,它允許您指定一個RegEx過濾器,您可以將其用於此目的。

0

你可以這樣做:

// In routes.php 
$rewrites = array(); 
$rewrites = am($rewrites, ClassRegistry::init('Article')->rewrites()); 
$rewrites = am($rewrites, ClassRegistry::init('AnotherModel')->rewrites()); 
$rewrites = am($rewrites, ClassRegistry::init('YetAnother')->rewrites()); 
foreach ($rewrites as $rewrite) { 
    Router::connect($rewrite[0], $rewrite[1], $rewrite[2]); 
} 

隨着deceze的方法,你只能有一個包羅萬象的。在這種方法中,你可以定義一個完整的堆棧。

雖然這種方法是有點怪異的,因爲你是從配置文件查詢模型。