CodeIgniter Routing

15,644

Solution 1

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.

Solution 2

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.

Solution 3

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";
Share:
15,644
Torez
Author by

Torez

Updated on June 07, 2022

Comments

  • Torez
    Torez almost 2 years

    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 getCategoryByName function for the first segment, then run the getProductByName for the second segment. Here is what I have:

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

    But its not working. "docupen" is the category, and "rc805" is the product.

    Thanks in advance.


    Thank you all for your help. This is what I ended up with for what I needed.

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