CodeIgniter Routing

I am developing a CI e-commerce website that has product categories and products. I want to redirect the url so that it goes to the product controller, then run the getCategoryByName function for the first segment, and then run getProductByName for the second segment. Here is what I have:

URL:
products / docupen / rc805
routes.php:
$ route ['products / ([az] +)'] = "products / getCategoryByName / $ 1";
$ route ['products / ([az] +) / ([a-z0-9] +)'] = "products / $ 1 / getProductByName / $ 2";

But it does not work. "docupen" is the category, and "rc805" is the product.

Thanks in advance.


Thank you all for your help. This is what I turned out to be for what I need.

$ route ['products /: any /: num'] = "products / getProductByID";
$ route ['products /: any /: any'] = "products / getProductByName";
$ route ['products /: any'] = "products / getCategoryByName";
+5
source share
3 answers

My answer is a bit like Colin's.

When I played with routes in CodeIgniter, I came to the conclusion that the order of the routes is important. When it finds the first valid route, it will not follow other routes in the list. If it does not find valid routes, it will handle the default route.

My routes that I played with my specific project worked as follows:

$route['default_controller'] = "main";
$route['main/:any'] = "main";
$route['events/:any'] = "main/events";
$route['search/:any'] = "main/search";
$route['events'] = "main/events";
$route['search'] = "main/search";
$route[':any'] = "main";

" http://localhost/index.php/search/1234/4321" main/search, $this->uri->segment(2); 1234.

( ):

$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";

, , (products/$1/getProductByName/$2), , URI. $this->uri->segment(n);, Colin , , .

+11

URI class "docupen" "rc805" URL-. , .

, URL- - www.yoursite.com/products/docupen/rc805, :

$category = $this->uri->segment(2); //docupen
$product = $this->uri->segment(3); //rc805

$category $product, .

+5

CodeIgniter routes do not work with regex. They are supported, I can not get them to work. It would be much easier to catch them.

$route['products/(:any)'] = "products/getCategoryByName/$1";
$route['products/(:any)/(:any)'] = "products/$1/getProductByName/$2";
+2
source