In the past couple days, a few folks from the CGI::App and Catalyst communities have posted examples of their dispatcher/controller layout and syntax. Here's what a similar example might look like in Jifty:
use warnings; use strict;
package MyApp::Dispatcher; use Jifty::Dispatcher -base;
under '/admin/account' => run { before '*' => set object_type => 'account'; before qr'/(\d+)' => run { set id => $1; };
on '/' => show 'crud/index'; on '/create' => show 'crud/create'; on '/*/update' => show 'crud/update'; on '/*' => show 'crud/view'; };
Can be written instead as:before '*' => run { set object_type => 'account' };
on '/' => run { show 'crud/index' };
it's a bit more declarative that way.before '*' => set object_type => 'account';
on '/' => show 'crud/index';
Redundant qr{...}
audreyt on 2006-11-29T19:51:11
Also, with our extended glob syntax one can replace:with this:before qr'/(.*?)/(.*)' => run { set id => $2; };With both changes in place, it'd look like this:before '**/*' => run { set id => $2; };under '/admin/account' => run {
before '*' => set object_type => 'account';
before '**/*' => run { set id => $2 };
on '/' => show 'crud/index';
on '/list' => show 'crud/list';
on '/create' => show 'crud/list';
on '/view/*' => show 'crud/view';
on '/update/*' => show 'crud/update;
};Re:Redundant qr{...}
jesse on 2006-11-29T19:56:47
Sorry. I changed some things out from under you. oops.
before qr'/(\d+)' => run { set id => $1; };
# hash, equivalent to
/(\d+)
before '/#' => run { set id => $1 }
# range, for more specific matching (bit more complicated to implement but worth it imo)
before '/[0-255]' => run { set id => $1 }
# alternatives, would make sense too:)
before '/{1|133|255}' => run { set id => $1 }
Re:Small suggestion
jesse on 2006-11-30T05:29:37
It's an interesting thought. Though once we get to ranges, it's almost easier to just use full regexps. (And everywhere we use globs, we can just use a full regexp and qr'' instead.
Globs are really nice for the simple case. I'm not quite sure where to draw the line. But "#" feels really nice. The suggestion is much appreciated.Re:Small suggestion
audreyt on 2006-11-30T05:35:49
We already support the standard glob syntax:As well as character classes:before '{1,133,255}'So I'm afraid that numeric range needs some other syntax. The '#' shorthand is very nice, though.before '[a-z]':-) Re:Small suggestion
sri on 2006-11-30T06:35:03
Ah, nice one, been some time since i last looked at Jifty::Dispatcher.:) Re:Small suggestion
audreyt on 2006-12-01T18:15:26
----------------------------------------------------------------------
r25678 (orig r2238): audreyt | 2006-12-01 12:43:40 +0800
* Jifty::Dispatcher - NUMBER SIGN (#) now captures one or more digit
characters in the extended shellglob condition syntax.
Suggested by: Sebastian Riedel
----------------------------------------------------------------------