我在建设有一个笨和tutorialsystem想实现下列网址结构:
- /教程 - >介绍的所有类别列表页
- /教程/ {类别作为字符串} - >这将给教程给定类别的清单,例如:/教程/ PHP
- /教程/ {类别作为字符串} / {的ID} / {段塞教程} - >此将显示教程,例如:/教程/ PHP / 123 /如何使用的函数
- /教程/添加 - >页面添加一个新的教程
问题是,当我想用前两种类型的网址,我需要将参数传递给控制器的索引功能。 第一个参数是可选的类别,第二个是可选的教程ID。 我已经做了一些研究,我贴过,所以我发现我可以添加喜欢的路线tutorials/(:any)
,但问题是,这条线路将通过add
作为参数使用过的最后一个URL时(/教程/加)。
任何想法我怎么能做到这一点?
你的路由规则可能会按以下顺序:
$route['tutorials/add'] = "tutorials/add"; //assuming you have an add() method
$route['tutorials/(:any)'] = "tutorials/index"; //this will comply with anything which is not tutorials/add
然后在您的控制器的index()方法,你应该能够制定出是否是该类别或教程ID被传递!
我认为,一个重映射必须是如果你想更多的方法添加到您的控制器,而不仅仅是“添加”更多地使用你的问题的。 这应该做的任务:
function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else {
$this->index($method);
}
}
发布几分钟后,我想我已经找到了这种可能的解决方案。 (可耻的是我)。
在伪代码:
public function index($cat = FALSE, $id = FALSE)
{
if($cat !== FALSE) {
if($cat === 'add') {
$this->add();
} else {
if($id !== FALSE) {
// Fetch the tutorial
} else {
// Fetch the tutorials for category $cat
}
}
} else {
// Show the overview
}
}
此解决方案的反馈是欢迎!