gpt4 book ai didi

php - 没有id的cakephp路由?

转载 作者:可可西里 更新时间:2023-10-31 22:49:53 26 4
gpt4 key购买 nike

有没有办法在 cake 中路由 url 而无需在 url 中包含 ID?

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

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

最佳答案

当然。这样做的唯一要求是 URL 中有足够的唯一信息来确定您想要的文章。如果 /article-name 在您的数据库中是唯一的,您可以使用它来查找您想要的特定记录。

在 config/routes.php 中:

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

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

在 controllers/articles_controller.php 中:

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

注意不要将您的产品命名为可以合法出现在 URL 中的任何名称,这样您就不会遇到冲突。 URL http://example.com/pages 是否指向产品“页面”或 array('controller' => 'pages', 'action' => 'index' )?为此,您还需要在 routes.php 中以一种允许首先访问所有 Controller 的方式定义您的路由,并且只有未定义的其余部分才会通过管道传输到您的 ArticlesController。看third parameter of Routes::connect ,它允许您指定可用于此目的的 RegEx 过滤器。

关于php - 没有id的cakephp路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1719199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com