Permalink
Browse files

Merge pull request #2 from doy/master

improve path::router example
  • Loading branch information...
miyagawa committed Oct 10, 2012
2 parents 55119ec + cd37b10 commit 7b9b55247199204a37669043f5843e275ace00d6
Showing with 7 additions and 15 deletions.
  1. +7 −15 path-router.psgi
View
@@ -1,30 +1,29 @@
-use Plack::Request;
-use Plack::Response;
-
use MyApp::Blog;
use MyApp::Hello;
use Path::Router;
+use Plack::App::Path::Router;
use Moose::Util::TypeConstraints;
sub dispatch {
- my ($r, $mapping) = @_;
+ my ($r) = @_;
+ my $mapping = $r->env->{'plack.router.match'}->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('' => (
+$router->add_route('/' => (
defaults => {
controller => 'Hello',
action => 'index',
},
target => \&dispatch
));
-$router->add_route('blog/:year/:month' => (
+$router->add_route('/blog/:year/:month' => (
defaults => {
controller => 'Blog',
action => 'monthly',
@@ -36,19 +35,12 @@ $router->add_route('blog/:year/:month' => (
target => \&dispatch
));
-$router->add_route('comment', => (
+$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 $res = $match->target->($req, $match->mapping);
- $res->finalize;
-};
+Plack::App::Path::Router->new(router => $router)->to_app;

0 comments on commit 7b9b552

Please sign in to comment.