有没有什么办法可以将网址中的网址路由到网址中,而无需在网址中输入ID?没有ID的cakephp路由?
所以不是www.mydomain.com/id/article-name我只想www.mydomain.com/article-name
我一直在关注这一点。 http://book.cakephp.org/view/543/Passing-parameters-to-action
有没有什么办法可以将网址中的网址路由到网址中,而无需在网址中输入ID?没有ID的cakephp路由?
所以不是www.mydomain.com/id/article-name我只想www.mydomain.com/article-name
我一直在关注这一点。 http://book.cakephp.org/view/543/Passing-parameters-to-action
当然。唯一的要求是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过滤器,您可以将其用于此目的。
你可以这样做:
// 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的方法,你只能有一个包罗万象的。在这种方法中,你可以定义一个完整的堆栈。
虽然这种方法是有点怪异的,因为你是从配置文件查询模型。