Every web framework out there has their own way of doing this, they all have advantages and disadvantages./foo/bar/index -> Foo::index() /test123.html -> Bar::default() /cookies/list/fresh -> Cookies::list()
Servlets 3.0 - Java# /foo/bar/index package Foo::Bar; sub index : Relative { ... } # /test123.html package Foo::Bar; sub test : Path('/test123.html') { ... } # /cookies/list/fresh package Cookies; sub list : Relative Args(1) { ... }
Jifty - Perl@Servlet(urlMappings={"/foo", "/bar"}) public class ControllerWithAnnotations { @GET public void handleGet(HttpServletRequest req, HttpServletResponse +res) { ... } }
Catalyst 4 - Perl# /foo/bar/index on 'foo/bar/index' => do { ... } # /test123.index on 'test123.html' => do { ... } # /cookies/list/fresh on '/cookies/list/*' => do { ... }
Ruby on Rails - Ruby__PACKAGE__->action('/foo/bar/index' => sub { ... });
Most interesting for me is the ability to reverse the route and generate urls from the pattern.# /foo/bar/index map.connect 'foo/bar/index', :controller => "foo", :action => "index" # /test123.html map.connect 'test123.html', :controller => "foo", :action => "test" # /cookies/list/fresh map.connect ':controller/:action/:quality', :controller => "cookies", +:action => "list", :quality => "fresh", :requirements => { :quality => /\w+/ }
Merb - Ruby<%= link_to "Fresh Cookies", :controller => "cookies", :action => "lis +t", :quality => "fresh" %>
Django - Python# /foo/bar/index r.match("/foo/bar/index").to(:controller => "foo", :action => "index") # /test123.html r.match("/test123.html").to(:controller => "foo", :action => "test") # /cookies/list/fresh r.match(%r[^/cookies/list/(\w+)$]).to(:controller => "cookies", :actio +n => "list", :quality => 'path[1]')
Mojolicious - Perlurlpatterns = patterns('', (r'^foo/bar/index$', 'foo.views.index'), (r'^test123\.html$', 'foo.views.test'), (r'^cookies/list/(\w+)$', 'cookies.views.list'), )
Note that this was just a very basic overview, all frameworks have much more features than i've shown here.# /foo/bar/index $r->route('/foo/bar/index')->to(controller => 'foo', action => 'index' +); # /test123.html $r->route('/test123.html')->to(controller => 'foo', action => 'test'); # /cookies/list/fresh $r->route('/:controller/:action/:quality', quality => qr/\w+/) ->to(controller => 'cookies', action => 'list', quality => 'fresh'); $c->url_for(controller => 'cookies', action => 'list', quality => 'fre +sh');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dispatchers for dummies
by Tanktalus (Canon) on Oct 14, 2008 at 21:24 UTC | |
by sri (Vicar) on Oct 14, 2008 at 22:46 UTC | |
|
Re: Dispatchers for dummies
by aufflick (Deacon) on Oct 16, 2008 at 00:52 UTC | |
|
Re: Dispatchers for dummies
by EvanCarroll (Chaplain) on Oct 18, 2008 at 17:10 UTC |