I am developing an ecommerce website with CI that has product categories and products. I want to route the URL so that it will go to the products controller, then run the ge
You should use the URI class to retrieve the "docupen" and "rc805" segments from your url. You can then use those values in whatever functions you need.
For example, if your url is www.yoursite.com/products/docupen/rc805, you would use the following in your products controller to retrieve the segments and assign them to variables:
$category = $this->uri->segment(2); //docupen
$product = $this->uri->segment(3); //rc805
Then you can use $category and $product however you need to.
My answer builds a bit on Colin's answer.
When I played around with the routes in CodeIgniter I came to the conclusion that the order of the routes was important. When it finds the first valid route it won't do the other routes in the list. If it doesn't find any valid routes then it will handle the default route.
My routes that I played around with for my particular 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";
If I entered "http://localhost/index.php/search/1234/4321" It would be routed to main/search and I can then use $this->uri->segment(2);
to retrieve the 1234
.
In your scenario I would try (order is very important):
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
I don't know enough to route the way you wanted (products/$1/getProductByName/$2
), but I'm not sure how you would create a controller to handle this particular form of URI. Using the $this->uri->segment(n);
statements as mentioned by Colin in your controller, you should be able to do what you want.
CodeIgniter routes don't work well with regex. They are supported, not I can never get them to work. It would be much easier to catch them like this
$route['products/(:any)'] = "products/getCategoryByName/$1";
$route['products/(:any)/(:any)'] = "products/$1/getProductByName/$2";