我正在使用 codeigniter 构建一个教程系统,并希望实现以下 URL 结构:
- /tutorials --> 包含所有类别列表的介绍页面
- /tutorials/{a Category as string} --> 这将给出给定类别的教程列表,例如/教程/php
- /tutorials/{a Category as string}/{an ID}/{tutorial slug} --> 这将显示教程,例如/tutorials/php/123/如何使用函数
- /tutorials/add --> 添加新教程的页面
问题是,当我想使用前两种类型的 URL 时,我需要将参数传递给控制器的索引函数。第一个参数是可选的类别,第二个参数是可选的教程 ID。我在发布之前做了一些研究,所以我发现我可以添加一条路线,例如tutorials/(:any)
,但问题是这条路线会经过add
使用最后一个 URL (/tutorials/add) 时也作为参数。
我有什么想法可以实现这一点吗?
您的路由规则可能按以下顺序排列:
$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 还是教程 ID!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)