Search code examples
phpcodeigniterurl-rewritingcodeigniter-urlcodeigniter-routing

How to make codeigniter user profile url look pretty


I am trying to figure out the best way to make a nice looking profile URL in codeigniter.

Normally i would just like to a controller with a third url paramter (the profile id) like so:

http://thesite.com/profile/4

======

This isn't going to work for the site i'm building now because I want a nice looking url with the company name, like so:

http://thesite.com/profile/some-company-name

=======

Their are 2 problems with this and maybe i'm just not thinking straight today and the answer is obvious, but if the url is a hyphenated version of the company name, if 2 of the same company are in the database for some weird reason, then the profile might not be the correct one, really the only good way is to provide a profile id and pull by the id, but then my url doesn't look good...

How would you handle this situation? I guess i could always link to /profile/id and then in the controller just look up the profile and redirect to /profile/company-name, but then the user that went to the site and typed in /profile/company-name would get a 404 page.

Any good ideas for me?


Solution

  • Just check the guide, it shows you with the URL Helper:

    Setup a model to handle your getter / setter (for profile name). Getter gets the proper content to display, setter sets the profile name and handles duplicates (same names by adding a 1 etc;). Think of a createive way to eliminate collisions, addding state name or zip if you don't want something ugly.

    Use url_title() to handle clean urls and eliminate odd characters:

    $title = "What's wrong with CSS?";
    $url_title = strtolower(url_title($title));
    // Produces: whats-wrong-with-css