您可以创建控制器函数来处理:
- 商店和商店页面
- 类别和类别页面
- 子类别和子类别页面
控制器功能
在您的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
. 这些路由会将 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";