Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In a fairly complicated web application, there are almost hundreds of redirecting to another page, or linking to another page, etc. Imagine that you have a page called /services/users/register-new-user and in many places you want to redirect users to this page, or you want to provide links to this page. However, if for any reason in future, you change this address to something like /users/register, you should update all links, otherwise they won't resolve. I'm finding myself always encountering this problem, since wherever I want to redirect or link to /services/users/register-new-user I hard-code the entire relative address.

How can I improve this pattern?

share|improve this question
1  
use a constant variable in a configuration class. If you do change the name of the page, its easy to resolve, you could even generate a small utility to create the class for you. Really the simply solution is to avoid trivial changes like changing the file name. –  Ramhound Aug 10 '11 at 12:07
    
Why don't you use Resources? –  Disciple Aug 10 '11 at 12:08
    
@Ramhound, what about a website with 10,000 pages? You can't create constants for every page. But the idea of a URL generator class is not bad. But I think there should be a better way. :) –  Saeed Neamati Aug 10 '11 at 12:14
add comment

1 Answer

up vote 4 down vote accepted

The most obvious thing that springs to mind is to hold the address in a variable and always use that when building the URL you need.

Then if you change the URL you just need to update the variable.

Other alternatives are to use a pattern like MVC routes to build up your addresses. Then you just need to update the logic that defines your routes.

share|improve this answer
    
Good suggestion @ChrisF. However, for languages that don't have route engine (like PHP, correct me if I'm wrong), writing a route engine is not very easy. Also for websites with great number of pages, storing URLs in variables doesn't sound being feasible. :) –  Saeed Neamati Aug 10 '11 at 12:16
2  
You can write routing in PHP, and it exists in many framework. Code igniter for exemple has a good support for routes. –  deadalnix Aug 10 '11 at 12:24
    
@Saeed - I can't think of any language that has a routing engine built in. A lot of frameworks do however. –  ChrisF Aug 10 '11 at 12:24
    
@Saeed: Why doesn't it sound feasible to store lots of URLs in variables? –  blubb Aug 10 '11 at 12:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.