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>');
}
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:
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):