Question

I have been tinkering with the idea of creating a routing system as a learning experience and not for some production purpose.

I have trying to decide what features are a must have and what features are best handled elsewhere in a system. I have come up with a short list of features and I would like to get some feedback on it. Are there things missing? Or are there anything that is just pure overkill? The list goes a follows:

  • Regular expression matching against URI
  • Matching HTTP request scheme
  • Matching HTTP request method
  • Checking if a request is a normal request or an AJAX request.

I have chosen to use regular expressions so that I have the possibility of using capture groups to match a dynamic URI. Regarding the overheat of regular expressions I think the worst can be reduced by producing clever expressions and only matching until a match is found.

Was it helpful?

Solution

Everyone has their own wishlist for routing. Which is why so many people have looked over the landscape, decided that everyone else's routing is bad, and written a new one.

That said, the top of my wishlist includes hierarchical routing. That is, based on the start of the URL you should be able to route to a component that takes the next component and routes that as well. This has a performance benefit (you don't have to check at the top level for every URL in components we know you are not in) and also a code modularity benefit (when working on one component, you do not need to be messing with code for other ones).

And the top of the wishlist for any SEO purposes is going to include the ability to have URL rewriting where external SEO friendly URLs which are maintained in a dynamic table get remapped to internal URLs that reflect your application structure.

And esoterically at the application level, I like modular components inside of pages. Which means that inside of a top level page you might have a component for a sidebar over here, another one for an ad unit over there, and so on. If you like that design, then you might want to consider reusing your routing logic both for finding a top level URL, and for finding a component inside of your application. One of the nice things about operating this way is that you can set up components as things that return a data structure, and that data structure can be both consumed inside of your application while generating a page, or exposed as JSON for an AJAX request. However this is idiosyncratic, and don't try it if it doesn't sound interesting to you.

Licensed under: CC-BY-SA with attribution
scroll top