Question

So I've taken over a project a friend of mine had started but didn't have the time to finish. It's an online e-commerce system built with CodeIgniter. The problem is, every product's url is setup like site.com/store/viewProduct?id=3 with the ID as the identifier. Now I am okay with keeping the ID in the URL, but I would like to make it so the ? is removed and the product title is also added in as well. So the final URL would be like site.com/store/viewProduct/3/cake-cutter or something like that.

The controller functions all reference to $_GET['id']; as well. I know I will have to change that as well... but to what?

I tried changing the URLs to like shown above and then using $this->uri->segment(1,0); to get the product id, but I got an error.

I will use CodeIgniters built in url_title(); function to generate the product names, but what is the easiest way to go about changing these urls?

Était-ce utile?

La solution

The best thing to do would just be to change the way the controller is getting your ID. I am assuming you don't need the 4th URI segment and that is just for SEO/usability purposes... but even if you do, you will still get it with the way this function is set up.

class Store extends CI_Controller {

    //site.com/store/viewProduct/3/cake-cutter
    public function viewProduct($id, $name)
    {
        //do stuff and now you have access to $id and $name
        //$id == 3
        //$name == 'cake-cutter'
    }

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top