codeigniter如何根据uri段的no设置路由到默认控制器 [英] codeigniter how to set routes to default controller according to the no of uri segments
问题描述
我在项目中使用codeigniter作为模块来显示类别及其项目的信息。
I am using codeigniter as a module in my project to display information as categories and its items.
我想要的URL如下所示
I want the url's like the following
- sitename.com/module/将以随机顺序显示所有项目。
- sitename.com/moduel/categoryname/显示该特定类别中的所有项目
- sitename.com/module/categoryname/title-of-the-item/这将显示该特定项目的详细信息
这里sitename.com/module/是codeigniter的根目录,默认控制器是home。
here sitename.com/module/ is the root of the codeigniter and the default controller is home.
我的问题是如何为一个段或uri和两个uri段设置路线
my question is how to set routes for one segment or uri and two segments of uri like
- sitename.com/module/应加载默认
- sitename.com/module/categoryname/应载入首页/类别/类别名称
- sitename.com/module/ categoryname / item-of-the-item应加载index / title / title-of-the-item
函数如下
- 函数索引()...
- 函数类别($ categoryname)。 ...
- function title($ title)....
我想根据段数创建一个路由,或者如果有任何其他替代来完成这个与路由,那是很好。
so, either i want to create a route according to the num of segments or if there is any other alternate to accomplish this with routing then that is fine.
我想使用
所以,
推荐答案
$route['page/(:any)'] = "home/page/$1";
$route['page'] = "home/index";
$route['(:any)/page/(:any)'] = "home/category/$1/page/$2";
$route['(:any)/page'] = "home/category/$1";
$route['page'] = "home/index";
$route['(:any)/(:any)'] = "home/title/$2";
$route['(:any)'] = "home/category/$1";
最后两行(7和8)是我需要的... (我的目的)
最后两行之前的行(1,2,3,4,5)是如何对最后两行使用分页7和8)
the lines before the last two(1,2,3,4,5) is how i use pagination for the last two lines(7 and 8)
只有首页/类别和首页/有分页,而不是首页/标题,即结束页。
only home/category and home/ has pagination and not home/title which is the end page.
同时,如果任何人发现代码可能是buggy或需要完美,那么请添加一个答案,以便我可以更新。
meanwhile, if anybody found the code could be buggy or need perfection then please add an answer so that i can update.
这篇关于codeigniter如何根据uri段的no设置路由到默认控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!