想出了一个方法来做到这一点。具体来说,我想使用看起来非常漂亮的 URL,以便用户可以输入类似http://example.com/aCategoryTitle/aTopicTitle/aSectionTitle/aSubsectionTitle之类的内容- 但需要验证此数据,以便该小节绝对属于正确的类别、主题等。
$conditions = array(
'Subsection.title' => $subsectionname,
'Section.title' => $sectionname,
);
$subsection = $this->Subsection->find('all', array(
'conditions' => $conditions,
'recursive' => 2,
));
这是一个简单的查找调用,它匹配具有正确标题和节标题的所有小节。
if (!$subsection) {
// nothing was returned
$this->redirect(array('controller' => 'KnowledgeBase', 'action' => 'index'));
}
如果没有符合该条件的小节,则重定向用户。但是,仍然可能会发现多个具有相同部分标题的子部分。以以下 URL 为例:
http://example.com/Cats/Feeding/Food/Meat和http://example.com/Dogs/Feeding/Food/Meat
通过这两个示例,将找到多个符合上述条件的小节。所以我们需要进行一些进一步的验证:
foreach ($subsection as $asubsection) {
if (($asubsection['Section']['Topic']['Category']['title'] == $categoryname) &&
($asubsection['Section']['Topic']['title'] == $topicname) &&
($asubsection['Section']['title'] == $sectionname) &&
($asubsection['Subsection']['title'] == $subsectionname)) {
// correct parameters
// this variable can now be passed to the view
$this->set('mysubsection', $asubsection);
} else {
// incorrect parameters in URL
$this->redirect(array('controller' => 'KnowledgeBase'));
}
}
这会循环遍历我们找到的所有 Subsections,并确保它们的标题以及它们所属的部分、主题和类别的标题与 URL 匹配(由 给出$this->request->params
)。
可能不是最整洁的方法,但它可以工作,并且与我的路由很好地配合。总是很高兴接受任何进一步的建议,但现在这对我来说效果很好。