Question

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?

Was it helpful?

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

OTHER TIPS

use urlencode and urldecode of php and routing add in route.php in config

$route['profile/(:any)'] = "controller_link/$1";

then you can use urldecode on your controller file like

public function controller_link($name=FALSE)
{
    if ($name != FALSE) {//check if name is passed 
        $queryname = urldecode($name);
        //then query on that name in the database
    }
}

if not clear then add comment.

Use unique name of company in Database, search your company by name, i use this function (helper) to translate name in slug.

if(!function_exists('slug')){
    function slug($text)
    {        
       // replace non letter or digits by -
       $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

       // trim
       $text = trim($text, '-');

       // transliterate
       $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);

       // lowercase
       $text = strtolower($text);

       // remove unwanted characters
       $text = preg_replace('~[^-\w]+~', '', $text);

       return (!empty($text)) ? $text : FALSE ; 

       }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top