1

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?

2
  • you want to dynamically change them? like a redirect? Commented Aug 15, 2013 at 22:40
  • I guess I'm asking what would be the best way to do this with minimal effort. I don't really know how I can do this without going through and basically rewriting the entire shopping cart class Commented Aug 15, 2013 at 22:43

1 Answer 1

3

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'
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.