The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Plack::App::Path::Router - A Plack component for dispatching with Path::Router

VERSION

version 0.05

SYNOPSIS

  use Plack::App::Path::Router;
  use Path::Router;

  my $router = Path::Router->new;
  $router->add_route('/' =>
      target => sub {
          my ($request) = @_;
          # use the Plack::Request to
          # create a Plack::Response ...
          my $response = $request->new_response( 200 );
          $response->content_type('text/html');
          $response->body('<html><body>HELLO WORLD</body></html>');
      }
  );
  $router->add_route('/:action/?:id' =>
      validations => {
          id => 'Int'
      },
      target => sub {
          # matches are passed to the target sub ...
          my ($request, $action, $id) = @_;
          # return a PSGI response ...
          [
            200,
            [ 'Content-Type' => 'text/html' ],
            [ '<html><body>', $action, $id, '</body></html>' ]
          ]
      }
  );
  $router->add_route('/:action/edit/:id' =>
      validations => {
          id => 'Int'
      },
      target => sub {
          my ($r, $action, $id) = @_;
          # return a string (we will wrap
          # it in a PSGI response for you)
          "This is my action($action), and I am editing this id($id)";
      }
  );
  $router->add_route('/foo' =>
      # target objects are also supported
      # as long as the object responds to
      # the ->execute method
      target => MyApp::Action->new( type => 'FOO' )
  );

  # now create the Plack app
  my $app = Plack::App::Path::Router->new( router => $router );

DESCRIPTION

This is a Plack::Component subclass which creates an endpoint to dispatch using Path::Router.

This module expects an instance of Path::Router whose routes all have a target that is a CODE ref or an object which responds to the execute method. The CODE ref or execute method will be called when a match is found and passed a Plack::Request instance followed by any path captures that were found. It is expected that the target return one of the following; an object which responds to the finalize method (like Plack::Response), a properly formed PSGI response or a plain string (which we will wrap inside a PSGI response with a status of 200 and a content type of "text/html").

This thing is dead simple, if my docs don't make sense, then just read the source (all ~75 lines of it).

ATTRIBUTES

router

This is a required attribute and must be an instance of Path::Router.

request_class

This is a class name used to create the request object. It defaults to Plack::Request but anything that will accept a PSGI-style $env in the constructor and respond correctly to path_info will work.

AUTHOR

Stevan Little <stevan.little at iinteractive.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Infinity Interactive.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.