Jos*_*osh 2 regex routing routes codeigniter
我一直在玩Codeigniter创建一个电子商务网站,我正在努力做到以下几点:
将类别名称作为第一个参数,例如
/tshirts/shoes以下每个都是过滤器(在类别上)或产品(在SEO的URL中有类别)
/tshirts/filter/price/0-20/tshirts/pink-with-blue-spots我目前在路由中做的是:
$route['tshirts'] = 'category/index';
$route['tshirts/(filter|sort)'] = 'category/index';
$route['tshirts/(filter|sort)/:any'] = 'category/index/filter';
$route['tshirts/:any'] = 'product/index';
Run Code Online (Sandbox Code Playgroud)
我想将前两行合并为一行,因为它们正在访问相同的控制器和方法.
第二行是在某人之后移除部件/filter/,并且可能与另一条路线合并.
最后一个路由表示第二个参数是产品名称,因此必须传递给产品控制器.
我一直在玩http://gskinner.com/RegExr/并提出以下内容,这可能很接近,我认为我只需要将括号中的部分作为选项(它正在拾取正确的路线,除了一个没有任何第二个参数),但我不知道如何.
category/?(filter|sort|page)
Run Code Online (Sandbox Code Playgroud)
谢谢!
今天快速浏览一下,现在已合并了
$route['tshirts/(filter|sort)'] = 'category/index';
$route['tshirts/(filter|sort)/:any'] = 'category/index/filter';
Run Code Online (Sandbox Code Playgroud)
成
$route['tshirts(/(filter|sort|page)(/(:any))?)?'] = "category/index/$4";
Run Code Online (Sandbox Code Playgroud)
在index($filter = null)类别控制器的方法中,用于$filter != null查看是否需要应用过滤规则.