views:

109

answers:

4

I've always found that defining URLs for other "pages" in a Web Application seems either clumbsy and fragile or bloated.

E.g. you have an application that manages customers, sales, prospects and thus has the following "pages".

  • customerlist
  • viewcustomer
  • editcustomer
  • addcustomer
  • viewcontact
  • editcontact
  • addcontact
  • prospect (x3? x4?)
  • sales (x?)
  • product (x?)
  • request (x?)
  • forecast (x?)
  • etc. ...

As an application grows, the number of pages grows to a list of 100+ URLs in no time.

Logic says that just copy/pasting these URLs in where needed on a page is ugly and a pain to maintain, but loading up 100+ key/values seems redundant if you only need 2 or 3 of them on "this" page.

Technically this is a language-agnostic (ASP,JSP,PHP,RoR,Python,etc.) question but I intend to implement in PHP (in an MVC setup). However if ASP.Net or Rails has a "really cool" way of doing this I'm all ears.

Update:

I'm mid-way through converting a non-MVC PHP app to use an MVC structure. Previously I had a big set of key'd links that were the base URL for a page, e.g.:

$URLs['CUSTOMER_ORDER_CONTACTS'] = '/customerordercontacts.php';
$URLs['CUSTOMER_PRODUCTS_EDIT'] = '/editcustomerproducts.php';
//etc.

Since I might be linking to the "Customer Products Edit" screen from anywhere, each page loaded this list, so that if/when the URL changed to another page... changing the item in the list would update all links.

However as I read the answers here I think I might have answered my own question by accident. What @Blixt wrote is pretty much the structure I have/plan to follow... at which point the URL is really structured in such a way that I don't need to have this list at all:

Example: If I am rendering a customer... and want to provide a link to each contact

//pseudo code
$contacts = $customer.getContacts();

//previous rendering list of links
foreach($contacts as $key => $value){
  echo('<a href="'.$URLs['CUSTOMER_CONTACT_VIEW'].'?customer='.$custID.'&contact='.$key.'">'.$value.'</a>');
}

//new rendering list of links
foreach($contacts as $key => $value){
  echo('<a href="/customer/'.$custID.'/contact/'.$key.'">'.$value.'</a>');
}
A: 

In CodeIgniter (PHP), they map URLs to objects and their methods, i.e.,

/customers/list/3

would call Customers->list('3'). You are flexible enough to overwrite this behaviour as needed for single URLs. In my opinion, this is a very nice way.

Django (Python) allows, nay, forces you, to do this manually. While this attempt gives you the greatest possible flexibility, mapping URLs via regular expressions to functions, it gets really complex for large applications.

Perhaps these methods help.

Cheers,

Boldewyn
After re-reading your question, I'm not sure anymore, if I got your point right. Did my answer provide any useful insights?
Boldewyn
+1  A: 

I like to keep the URLs as logical as possible, and avoid framework-specific URLs (consider that you may change the framework in the future, which would then have to keep using your old URL patterns to avoid breaking external links.)

Basically:

# All customers:
/customers/
# New customer:
/customers/new
/customers/?new # another version if you don't think "new" deserves its own path
# Existing customer:
/customers/123/
/customers/123 # another version if you want the concept that it's a page, not a
               # directory
# Edit customer:
/customers/123/edit
/customers/123?edit

# Some sub-list for a customer:
/customers/123/orders/

# and so on...

To be honest, I personally prefer the second example in the cases where I provided two examples. It uses the concept from file systems that lists are "directories" and separate entries are "files". Every path represents some kind of data, and other paths are separated using the query string (/customers/?new).

After you've decided on your URL structures, you should have a look at how to implement this in your web framework. Most frameworks I consider "good" have some kind of support for mapping URLs (usually with some kind of pattern supporting wildcards, such as regular expressions.) So your patterns might look like this, using my second examples (regex):

^/customers/$ -> CustomerList
^/customers/(\d+)$ -> Customer # This will check the query string for "edit" etc
                               # to determine which mode to use.
^/customers/(\d+)/orders/$ -> OrderList
Blixt
Your answer helped me clarify that the best strategy for my "need" is no longer relevant. Having a base url for each given page doesn't make sense anymore. Thanks for the clear perspective.
scunliffe
A: 

in PHP i'll go for a simple setup like this:

www.example.com/application/controller/action/parameter

where application must be a directory

controller must be a subdir under it's parent application

action must be a method in controller (default) OR can be another subdir under application->controller->action because sometimes i need to separate methods and real files.

then, parameter can be passed to the valid applications valid controllers valid action

for me, that was the most dynamic setup i can achieve.

if you like real-world examples pls ask me.

fabrik
A: 

The structure of URLs should take search engine optimization into account. Even if the site is only running internal to an organization, it's probably being crawled by an internal engine or will be at some point in the future. Here are a couple of key points from 2 Google resources I have found useful:

Good practices for URL structure

from http://www.google.com/webmasters/docs/search-engine-optimization-starter-guide.pdf:

Use words in URLs - URLs with words that are relevant to your site's content and structure are friendlier for visitors navigating your site. Visitors remember them better and might be more willing to link to them.

Provide one version of a URL to reach a document - To prevent users from linking to one version of a URL and others linking to a different version (this could split the reputation of that content between the URLs), focus on using and referring to one URL in the structure and internal linking of your pages.

URL structure

from http://www.google.com/support/webmasters/bin/answer.py?answer=76329:

A site's URL structure should be as simple as possible. Consider organizing your content so that URLs are constructed logically and in a manner that is most intelligible to humans (when possible, readable words rather than long ID numbers).

Consider using punctuation in your URLs. The URL http://www.example.com/green-dress.html is much more useful to us than http://www.example.com/greendress.html. We recommend that you use hyphens (-) instead of underscores (_) in your URLs.

Overly complex URLs, especially those containing multiple parameters, can cause a problems for crawlers by creating unnecessarily high numbers of URLs that point to identical or similar content on your site. As a result, Googlebot may consume much more bandwidth than necessary, or may be unable to completely index all the content on your site.

Mike Knowles