#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171

    A question about "Multiple word" names in friednly URL


    Hi;

    Ther is a table with list of place names like surfers paradise, mermaid beach, cooloongata airport, mainbeach etc.

    1 - Would it better practise to simply make the URLs using id or names?

    For example

    www.devshed.com/rentals/332

    VS

    www.devshed.com/rentals/southport

    Common sense tells me ids are better because it is straight forward int and simple queires, but I see many websites use string.

    2-How can I make the URL friednly for such "multiple word" names? The names are stored in the database with white space. For example: main beach.


    3 - Whichone is prefered? - or + or _ or something else?


    For example:

    www.devshed.com/rentals/mermaid-beach

    www.devshed.com/rentals/mermaid+beach

    www.devshed.com/rentals/mermaid_beach

    Thank you
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    1. Por que no los dos? IDs are easier to code to but names are more people-friendly.

    2. Suggested practice is to keep a second field for just the "URL title": the regular title is "Main Beach" and the URL title is some function of the regular one (eg, lowercase and replace spaces with hyphens, "main-beach").

    3. Hyphen versus underscore is debatable. IMO hyphens are better as they're not typically associated with words, like how regex's \w is letters, numbers, and underscores. Plus they're easier to type (no shift key) and they just look better.
    You could use plus but... no. It just feels awkward.
    Code:
    www.example.com/rentals/332-main-beach
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    1. Por que no los dos?
    Didn't think it's an option.

    1 - First put id,
    2 - The replace space with dahs,
    3 - Done

    Thanks
  6. #4
  7. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Sorry I forgot to ask this:

    I usually name my controllers with underscore. For example:

    sign_in or new_user

    That way if there are arguments in URL it doesnt looks consistent.

    www.devshed.com/sing_in/south-goldcoast

    I found some solutions but there are tones of bad info there.

    What to do?
    Last edited by zxcvbnm; August 13th, 2013 at 09:27 PM.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Solutions to what? Having a front controller that can't replace hyphens with underscores when determining a class name?
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    Solutions to what? Having a front controller that can't replace hyphens with underscores when determining a class name?
    I read in a few places to use rout file in config to replace dashes with underscores, I am not sure if it is a good idea.
  12. #7
  13. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Uh, maybe? I'd have to see exactly what they said. I mean, the overall idea of simple replacements on the name to get the controller class is so standard it's practically a requirement, but it might depend on what the "router file" or "config" are exactly.

    Like
    PHP Code:
    // if the url is /controller/controller/controller/method

    $controllername dirname(strtok($_SERVER["REQUEST_URI"], "?"));
    $controllerclass "page_" strtr($controllername"/-""__");
    if (
    class_exists($controllerclass)) {
        
    $controller = new $controllerclass();
        
    // ...
    } else {
        
    // 404

    (validation aside, of course)
  14. #8
  15. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,688
    Rep Power
    171
    Originally Posted by requinix
    Uh, maybe? I'd have to see exactly what they said.
    PHP Code:
    $route['about-us'] = "about_us"
    Source.
    PHP Code:
    <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    /*
    | -------------------------------------------------------------------------
    | URI ROUTING
    | -------------------------------------------------------------------------
    | This file lets you re-map URI requests to specific controller functions.
    |
    | Typically there is a one-to-one relationship between a URL string
    | and its corresponding controller class/method. The segments in a
    | URL normally follow this pattern:
    |
    |    example.com/class/method/id/
    |
    | In some instances, however, you may want to remap this relationship
    | so that a different class/function is called than the one
    | corresponding to the URL.
    |
    | Please see the user guide for complete details:
    |
    |    http://codeigniter.com/user_guide/general/routing.html
    |
    | -------------------------------------------------------------------------
    | RESERVED ROUTES
    | -------------------------------------------------------------------------
    |
    | There area two reserved routes:
    |
    |    $route['default_controller'] = 'welcome';
    |
    | This route indicates which controller class should be loaded if the
    | URI contains no data. In the above example, the "welcome" class
    | would be loaded.
    |
    |    $route['404_override'] = 'errors/page_missing';
    |
    | This route will tell the Router what URI segments to use if those provided
    | in the URL cannot be matched to a valid route.
    |
    */

    $route['default_controller'] = "welcome";
    $route['404_override'] = '';


    /* End of file routes.php */
    /* Location: ./application/config/routes.php */
  16. #9
  17. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,295
    Rep Power
    9400
    Oh. Then they're not doing anything magical - it's just an array of URLs and class names.

    If you want hyphens in the name then it looks like you need $route. If you want underscores (ie, the same name as the class) then the routing thing might handle it automatically.
  18. #10
  19. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4124
    Also, ask in the SEO forum if you feel it's relevant.

    They'll probably say use a hyphen ("-") instead of an underscore ("_") to represent spaces and beware the over-optimisation penalty (it used to be the case to try to get your url to match your title tag to match the first h1 tag)
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo