NAME

Router::Simple - simple HTTP router

SYNOPSIS

use Router::Simple;

my $router = Router::Simple->new();
$router->connect('/', {controller => 'Root', action => 'show'});
$router->connect('/blog/{year}/{month}', {controller => 'Blog', action => 'monthly'});

my $app = sub {
    my $env = shift;
    if (my $p = $router->match($env)) {
        # $p = { controller => 'Blog', action => 'monthly', ... }
    } else {
        [404, [], ['not found']];
    }
};

DESCRIPTION

Router::Simple is a simple router class.

Its main purpose is to serve as a dispatcher for web applications.

Router::Simple can match against PSGI $env directly, which means it's easy to use with PSGI supporting web frameworks.

HOW TO WRITE A ROUTING RULE

plain string

$router->connect( '/foo', { controller => 'Root', action => 'foo' } );

:name notation

$router->connect( '/wiki/:page', { controller => 'WikiPage', action => 'show' } );
...
$router->match('/wiki/john');
# => {controller => 'WikiPage', action => 'show', page => 'john' }

':name' notation matches qr{([^/]+)}.

'*' notation

$router->connect( '/download/*.*', { controller => 'Download', action => 'file' } );
...
$router->match('/download/path/to/file.xml');
# => {controller => 'Download', action => 'file', splat => ['path/to/file', 'xml'] }

'*' notation matches qr{(.+)}. You will get the captured argument as an array ref for the special key splat.

'{year}' notation

$router->connect( '/blog/{year}', { controller => 'Blog', action => 'yearly' } );
...
$router->match('/blog/2010');
# => {controller => 'Blog', action => 'yearly', year => 2010 }

'{year}' notation matches qr{([^/]+)}, and it will be captured.

'{year:[0-9]+}' notation

$router->connect( '/blog/{year:[0-9]+}/{month:[0-9]{2}}', { controller => 'Blog', action => 'monthly' } );
...
$router->match('/blog/2010/04');
# => {controller => 'Blog', action => 'monthly', year => 2010, month => '04' }

You can specify regular expressions in named captures.

regexp

$router->connect( qr{/blog/(\d+)/([0-9]{2})', { controller => 'Blog', action => 'monthly' } );
...
$router->match('/blog/2010/04');
# => {controller => 'Blog', action => 'monthly', splat => [2010, '04'] }

You can use Perl5's powerful regexp directly, and the captured values are stored in the special key splat.

METHODS

AUTHOR

Tokuhiro Matsuno <tokuhirom AAJKLFJEF@ GMAIL COM>

THANKS TO

Tatsuhiko Miyagawa

Shawn M Moore

routes.py.

SEE ALSO

Router::Simple is inspired by routes.py.

Path::Dispatcher is similar, but so complex.

Path::Router is heavy. It depends on Moose.

HTTP::Router has many dependencies. It is not well documented.

HTTPx::Dispatcher is my old one. It does not provide an OO-ish interface.

THANKS TO

DeNA

LICENSE

Copyright (C) Tokuhiro Matsuno

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