Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter句柄URL段_Codeigniter_Url_Segment - Fatal编程技术网

CodeIgniter句柄URL段

CodeIgniter句柄URL段,codeigniter,url,segment,Codeigniter,Url,Segment,我一直在思考如何用CI处理URL和页面,但我想不出一个好的方法 我正在尝试这样处理url: 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) 有什么好办法吗?您可以创建控制器函数来处理: 商店和商店页面 类别和类别页面

我一直在思考如何用CI处理URL和页面,但我想不出一个好的方法

我正在尝试这样处理url:

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)

有什么好办法吗?

您可以创建控制器函数来处理:

  • 商店和商店页面
  • 类别和类别页面
  • 子类别和子类别页面
控制器功能

商店
控制器中,您可以具有以下功能:

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";

在子类别函数中。。。我怎么知道猫的名字?@fred.kassi你想让类别、子类别和页面都成为参数吗?我想是的。。。site.com/shop/smartphone/apple或site.com/shop/smartphone/34或site.com/shop/54/Ok,我想我的答案现在可以满足您的要求了?没问题,我很高兴我的答案有帮助!