Welcome Guest, Not a member yet? Register   Sign In
How to create URL like "" www.domain.com/products/blue-shirts "" ?
#1

[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.
#3

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

Thanks for your replay.
#4

[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.
    }
  }
}
#5

[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.




Theme © iAndrew 2016 - Forum software by © MyBB