CodeIgniter Forums
How to create URL like "" www.domain.com/products/blue-shirts "" ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to create URL like "" www.domain.com/products/blue-shirts "" ? (/showthread.php?tid=59928)



How to create URL like "" www.domain.com/products/blue-shirts "" ? - El Forum - 12-11-2013

[eluser]Kiran K[/eluser]
Hi all,

I am developing a e-commerce website and i wanted to show url like this "www.domain.com/products/blue-shirts" , how can i achieve this in Codeigniter ??

How can i map the url slug ("blue-shirts") to its unique ID in my product table. ?

Please help me.
Thanks.

Kiran.


How to create URL like "" www.domain.com/products/blue-shirts "" ? - El Forum - 12-11-2013

[eluser]CroNiX[/eluser]
This should help:
http://ellislab.com/codeigniter/user-guide/general/controllers.html#remapping
http://ellislab.com/codeigniter/user-guide/general/routing.html


How to create URL like "" www.domain.com/products/blue-shirts "" ? - El Forum - 12-11-2013

[eluser]Kiran K[/eluser]
I will check that pages.

Thanks for your replay.


How to create URL like "" www.domain.com/products/blue-shirts "" ? - El Forum - 12-11-2013

[eluser]CroNiX[/eluser]
Something like this would probably work

/config/routes.php
Code:
$route['products/(:any)' = 'products/view/$1';

/controllers/products.php
Code:
class Products extends CI_Controller {

  function view($product_name = '')  //$product_name will be whatever $1 was in the route
  {
    if (empty($product_name))
    {
        //no product passed, must have just gone to "yoursite.com/products"
    }
    else
    {
      $product_id = $this->your_model->get_product_by_name($product_name);
      //now you should have your ID and can do what you need.
    }
  }
}



How to create URL like "" www.domain.com/products/blue-shirts "" ? - El Forum - 12-11-2013

[eluser]CroNiX[/eluser]
Code:
$route['products/(:any)' = 'products/view/$1';
$route['products'] = 'products/view_all';  //If there was no product passed, it will call this "view_all" method where you can list all products

Routes with the most segments need to come before those with fewer, like above. First route has 2 segments, "products/some-product" and the 2nd one only has 1, "products". So it should go in that order or the routing won't work properly.