Permalink
Browse files

Add Path::Router

    This is my olive branch to Stevan ;)
  • Loading branch information...
1 parent d5cea7f commit 9080a268d1bd3a5e1d75ca5730078f8b4aa4ee94 @sartak sartak committed Mar 16, 2010
Showing with 41 additions and 0 deletions.
  1. +41 −0 path-router.pl
View
41 path-router.pl
@@ -0,0 +1,41 @@
+use Plack::Request;
+use Plack::Response;
+
+use MyApp::Blog;
+use MyApp::Hello;
+
+use Path::Router;
+
+my $router = Path::Router->new;
+$router->add_route('' => (
+ defaults => {
+ controller => 'Hello',
+ action => 'index',
+ },
+));
+
+$router->add_route('blog/:year/:month' => (
+ defaults => {
+ controller => 'Blog',
+ action => 'monthly',
+ },
+));
+
+$router->add_route('comment', => (
+ defaults => {
+ controller => 'Blog',
+ action => 'comment',
+ },
+));
+
+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});
+ my $res = $controller->$action($req, $mapping);
+ $res->finalize;
+};

0 comments on commit 9080a26

Please sign in to comment.