我一直在考虑如何处理带有CI的网址和网页,但我无法想到这样做的好方法。
我正试图像这样处理网址:
site.com/shop (shop is my controller)
site.com/shop/3 (page 3)
site.com/shop/cat/4 (categorie and page 4)
site.com/shop/cat/subcat/3 (cat & subcat & page)
有没有好办法呢?
答案 0 :(得分:1)
您可以创建要处理的控制器功能:
控制器功能
在shop
控制器中,您可以使用以下功能:
function index($page = NULL)
{
if ($page === NULL)
{
//load default shop page
}
else //check if $page is number (valid parameter)
{
//load shop page supplied as parameter
}
}
function category($category = NULL, $page = 1)
{
//$page is page number to be displayed, default 1
//don't trust the values in the URL, so validate them
}
function subcategory($category = NULL, $subcategory = NULL, $page = 1)
{
//$page is page number to be displayed, default 1
//don't trust the values in the URL, so validate them
}
<强>路由强>
然后,您可以在application/config/routes.php
中设置以下routes。这些路由将URL映射到适当的控制器功能。正则表达式允许查找值
//you may want to change the regex, depending on what category values are allowed
//Example: site.com/shop/1
$route['shop/(:num)'] = "shop/index/$1";
//Example: site.com/shop/electronics
$route['shop/([a-z]+)'] = "shop/category/$1";
//Example: site.com/shop/electronics/2
$route['shop/([a-z]+)/(:num)'] = "shop/category/$1/$2";
//Example: site.com/shop/electronics/computers
$route['shop/([a-z]+)/([a-z]+)'] = "shop/subcategory/$1/$2";
//Example: site.com/shop/electronics/computers/4
$route['shop/([a-z]+)/([a-z]+)/(:num)'] = "shop/subcategory/$1/$2/$3";