Damchey Lhendup
Damchey Lhendup

Reputation: 33

pretty urls instead of id in php/codeigniter

I am developing a PHP site using codeigniter, and would like some help in how to go about achieving clean urls instead of numeric ids.

For example, instead of www.site.com/category/view/1 , i want something like www.site.com/category/view/automobiles

category = class
view = method
1 = variable

My Research
I am new to PHP and codeigniter, and so far how I wanted to achieve this was, to retrieve the uri->segment(3) and then check the DB for the corresponding ID in the database. For that, I would probably need to use unique category names, which I will.

My Problem
But my problem is, if the category name has special characters or have spaces in the string, what will happen? I'd like to convert the spaces into hyphens or underscores, and omit any other special characters. And while retrieving, i would like to decode the URI segment to it's original form so I can check the database for the ID.

Upvotes: 2

Views: 1254

Answers (2)

Valour
Valour

Reputation: 810

You need to just add a new column to your DB named "clean-url" and index it. Then when adding new category of something, build a clean url string from the name with a function.

function clean_url($string)
{
    return str_replace(array('+', '!', '&'), '_', $string);
}

You can add the chars you don't want it. Then search in the DB for matching stings in your controller.

This is the how Wordpress works.

Upvotes: 0

Tareq
Tareq

Reputation: 2006

You should not use any special characters but hyphens so that you need no decoding. You can use DB for your requirement as you describe.

Upvotes: 1

Related Questions