CodeIgniter handle URL segments -
i keep thinking how deal urls , pages ci can't think way this.
i'm trying handle url this:
site.com/shop (shop controller) site.com/shop/3 (page 3) site.com/shop/cat/4 (categorie , page 4) site.com/shop/cat/subcat/3 (cat & subcat & page)
is there way ?
you create controller functions handle:
- shop , shop pages
- categories , category pages
- subcategories , subcategory pages
controller functions
in shop
controller have following functions:
function index($page = null) { if ($page === null) { //load default shop page } else //check if $page number (valid parameter) { //load shop page supplied parameter } } function category($category = null, $page = 1) { //$page page number displayed, default 1 //don't trust values in url, validate them } function subcategory($category = null, $subcategory = null, $page = 1) { //$page page number displayed, default 1 //don't trust values in url, validate them }
routing
you setup following routes in application/config/routes.php
. these routes map urls appropriate controller functions. regex allow values
//you may want change regex, depending on category values 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";
Comments
Post a Comment