|Do you know where your variables are?|
Dispatchers for dummiesby sri (Vicar)
|on Oct 14, 2008 at 16:10 UTC||Need Help??|
(A little meditation from my blog.)
The most fun for me when building a new web framework is always the dispatcher design.
Thats one of the main reasons why Mojo is completely dispatcher agnostic.
Now you might be asking "WTF is a dispatcher?".
It is the part of your framework that maps incoming requests to the actual code that builds the response for it.
A very naive way of doing this would be a simple list of paths with classes and methods mapped to them.
Every web framework out there has their own way of doing this, they all have advantages and disadvantages.
Catalyst 5 - Perl
Lets start with my good old Catalyst 5.
Meta data is kept close to the code which makes initial development easier, but for debugging you depend very much on good log output.
Changing paths later on can be painful, and using attributes is also very tricky because the Perl api for them sucks, it can look quite good though if used right.
Servlets 3.0 - Java
Servlets 3.0 will do something similar with annotations. (I'm not going to give you full examples here because...well...it's Java and ugly as hell)
Jifty - Perl
For quite some time i was a big fan of declarative dispatchers like the Jifty one.
Meta data and code are once again close together, but you have to use a full blown domain specific language.
You can't use basic Perl features like inheritance and need to reinvent a lot.
The flow is easier to follow than in Catalyst though without using log output.
Catalyst 4 - Perl
Fun fact, we nearly built a declarative dispatcher for Catalyst 4, a long time before it was cool. :)
Ruby on Rails - Ruby
Ruby on Rails originally started using a static "/controller/action/args" mapping with Apache's mod_rewrite in front of it.
To get rid of the Apache dependency they've invented one of the imo coolest dispatcher concepts out there called Routes.
Routes separate meta data and code completely, so you can use all the basic language features in your code.
For easy initial development you start with a simple default route like ":controller/:action/:id" and later move on to more complicated url mappings.
Most interesting for me is the ability to reverse the route and generate urls from the pattern.
Merb - Ruby
Merb uses an alternative Routes implementation, with a lot of cool new ideas.
Django - Python
The prize for the ugliest dispatcher goes to Python's Django, which uses plain old regex for everything.
Mojolicious - Perl
For Mojolicious i'll be using a Perl-ish Routes implementation with some Catalyst goodness added.
More about that in my next article. ;)
Note that this was just a very basic overview, all frameworks have much more features than i've shown here.
But i hope it will help all you future framework developers, building dispatchers is fun, get Mojo and start today!