CodeIgniter - How Do I Rewrite URLs with URL Parameters

I need to turn URL's such as the following:

/catalog/products/24

into cleaner URL's such as the following:

/catalog/product-name-here

I'm using CodeIgniter, and rewrite engine is on and setup. I also setup rewriting in the routes.php file as follows:

$route['catalog/products/(:any)'] = 'catalog/view/$1';

Question: Where do I replace the product ID /24 with /product-name-here? I can successfully get the parameter $1, and fetch the product name. I can't seem to figure out how to rewrite the URL with this dynamic value though. Thanks in advance!

13.10.2009 14:59:13
2 ОТВЕТА
РЕШЕНИЕ

I'm not a codeigniter user but I smell that URL rewriting is not the best place to achieve this.

You still need to write a controller method that can identify a product using something like slug='product-name-here' instead of id=24.

2
13.10.2009 15:10:31

Just store the products name as a URL slug into your product row in the database table. Good practice for that the url_title() method from the URL Helper.

Then you just have to search for the slug from your database in your code, rather than the id.

So this is just a hint because I don't have the time right now. But if you've got questions, ask it here, I will come back to answer it.

And sorry for my poor English! :)

0
10.01.2019 10:49:24