Browse files

using the target attribute to attach a dispatcher

  • Loading branch information...
1 parent 2dfcc9a commit bab850556f86eb62de06c671bb379b25567ca60c Stevan Little committed Mar 16, 2010
Showing with 13 additions and 6 deletions.
  1. +13 −6 path-router.psgi
View
19 path-router.psgi
@@ -7,12 +7,21 @@ use MyApp::Hello;
use Path::Router;
use Moose::Util::TypeConstraints;
+sub dispatch {
+ my ($r, $mapping) = @_;
+ my $controller = 'MyApp::' . $mapping->{controller};
+ my $action = $controller->can(lc($r->method) . '_' . $mapping->{action})
+ or return $r->new_response(405);
+ $controller->$action($r, $mapping);
+}
+
my $router = Path::Router->new;
$router->add_route('' => (
defaults => {
controller => 'Hello',
action => 'index',
},
+ target => \&dispatch
));
$router->add_route('blog/:year/:month' => (
@@ -23,25 +32,23 @@ $router->add_route('blog/:year/:month' => (
validations => {
year => subtype( as 'Int' => where { $_ > 0 } ),
month => subtype( as 'Int' => where { $_ >= 1 && $_ <= 12 } ),
- }
+ },
+ target => \&dispatch
));
$router->add_route('comment', => (
defaults => {
controller => 'Blog',
action => 'comment',
},
+ target => \&dispatch
));
sub {
my $req = Plack::Request->new(shift);
my $match = $router->match($req->path_info)
or return $req->new_response(404)->finalize;
- my $mapping = $match->mapping;
- my $controller = "MyApp::" . $mapping->{controller};
- my $action = $controller->can(lc($req->method) . "_" . $mapping->{action})
- or return $req->new_response(405)->finalize;
- my $res = $controller->$action($req, $mapping);
+ my $res = $match->target->($req, $match->mapping);
$res->finalize;
};

0 comments on commit bab8505

Please sign in to comment.