0

现在我有两张桌子。类别和网站。我有一个名为 Cars (id:5) 的类别和两个属于该类别的网站(bmw.com、toyota.com)。如果我目前想查看所有 Cars 的网站,我使用正常的 url /categories/view/5。但我想要一个像 /categories/cars/ 这样的 url 来查看同一页面。所以基本上我需要类别表中的一行名称,而不是一个动作。

你能帮我存档吗?谢谢干杯

4

2 回答 2

1

这很容易 - 只需阅读有关路由的非常深入的文档,您将在几分钟内解决它:

http://book.cakephp.org/2.0/en/development/routing.html

于 2013-08-20T06:18:58.017 回答
1

为了实现您的要求,您需要对两件事进行更改:

  • app/Config/route.php文件。
  • app/Controller/CategoriesController.php文件。

app/Config/route.php

Router::connect('/categories/:name', array(
       'controller' => 'categories', 'action' => 'viewByName'
    ),
    array(
        'pass' => 'name',
        'name' => '[a-zA-Z0-9]+'
    )
);

app/Controller/CategoriesController.php

public function viewByName($name = null) {
    $option = array(
        'conditions' => array('Category.name'=>$name)
    );
    $category = $this->Category->find('first', $options);
    $this->set('category', $category);
}

然后你可以复制app/View/Categories/view.ctpinto的内容viewByName.ctp并稍微润色一下。

我给你最低限度,所以出于安全原因等原因,请添加检查变量内容。

您还可以通过查看Cake Documentation的Routing页面对此有更多的了解。

于 2013-08-20T06:40:11.717 回答