0

I keep thinking about how to deal with URLs and pages with CI but I can't think to a good way to do this.

I'm trying to handle url like this:

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)

Is there any good way to do this ?

4

1 回答 1

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. 这些路由会将 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";
于 2013-05-14T17:03:45.853 回答