2009-11-12 64 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的方法,你只能有一个包罗万象的。在这种方法中,你可以定义一个完整的堆栈。

虽然这种方法是有点怪异的,因为你是从配置文件查询模型。