cakephp路由没有id?

pin*_*ger 4 php cakephp

有没有办法在没有网址中的ID的情况下在网址中路由网址?

所以我只想要www.mydomain.com/article-name而不是www.mydomain.com/id/article-name

我一直在关注这个. http://book.cakephp.org/view/543/Passing-parameters-to-action

dec*_*eze 6

当然.对此的唯一要求是URL中有足够的唯一信息来确定您想要的文章.如果/article-name数据库中是唯一的,则可以使用它来查找所需的特定记录.

在config/routes.php中:

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

Router::connect('/*', array('controller' => 'articles', 'action' => 'view'));
Run Code Online (Sandbox Code Playgroud)

在controllers/articles_controller.php中:

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

请注意不要将您的产品命名为可以合法地出现在URL中的任何内容,这样您就不会遇到冲突.URL是http://example.com/pages指向产品"页面"还是指向array('controller' => 'pages', 'action' => 'index')?为此,您还需要以routes.php允许首先访问所有控制器的方式定义您的路由,并且只有未定义的其余部分通过管道传输到您的路径中ArticlesController.查看第三个参数Routes::connect,它允许您指定可用于此目的的RegEx过滤器.