Router::Simple - simple HTTP router
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)) { return "MyApp::C::$p->{controller}"->can($p->{action})->($env, $p->{args}); } else { [404, [], ['not found']]; } };
Router::Simple is simple router class.
The main purpose is dispatcher for web application.
Router::Simple is PSGI friendly.
$router->connect( '/foo', { controller => 'Root', action => 'foo' } );
$router->connect( '/wiki/:page', { controller => 'WikiPage', action => 'show' } ); ... $router->match('/wiki/john'); # => {controller => 'WikiPage', action => 'show', args => { page => 'john' } }
':name' notation matches qr{([^/]+)}.
$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 got captured arguments as 'splat'.
$router->connect( '/blog/{year}', { controller => 'Blog', action => 'yearly' } ); ... $router->match('/blog/2010'); # => {controller => 'Blog', action => 'yearly', args => { year => 2010 } }
'{year}' notation matches qr{([^/]+)}, and it will capture to 'args'.
$router->connect( '/blog/{year:[0-9]+}/{month:[0-9]{2}}', { controller => 'Blog', action => 'monthly' } ); ... $router->match('/blog/2010/04'); # => {controller => 'Blog', action => 'monthly', args => { year => 2010, month => '04' } }
You can specify the regexp for named capture.
$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.
create new instance of Router::Simple.
Add new rule for $router.
$router->connect( '/', { controller => 'Root', action => 'index' } ); $router->connect( 'show_entry', '/blog/:id', { controller => 'Blog', action => 'show' } ); $router->connect( '/blog/:id', { controller => 'Blog', action => 'show' } ); $router->connect( '/comment', { controller => 'Comment', action => 'new_comment' }, {method => 'POST'} );
define the new route to $router.
You can specify the $destination as \%hashref or \&coderef.The coderef should have keys named controller and action.
You can specify some optional things to \%options.Current version supports 'method' and 'host'. 'method' is ArrayRef[String] or String, that matches REQUEST_METHOD in $req. 'host' is String or Regexp, that matches HTTP_HOST in $req.
$router->submapper('/entry/, controller => 'Entry') $router->submapper(path_prefix => '/entry/, controller => 'Entry')
This method is shorthand for creating new instance of Router::Simple::Submapper.
The arguments will pass to Router::Simple::SubMapper->new(%args).
Match a URL against against one of the routes contained.
$req is a PSGI's $env or plain string.
This method returns a plain hashref.
If you are using +{ controller => 'Blog', action => 'daily' } style, then you got return value as following:
{ controller => 'Blog', action => 'daily', args => { year => 2010, month => '03', day => '04' }, }
If you are using sub { ... } as action, you will get the following:
{ code => sub { 'DUMMY' }, args => { year => 2010, month => '03', day => '04' }, }
Will return undef if no valid match is found.
generate path string from rule named $anchor.
You must pass the each parameters to \%opts.
my $router = Router::Simple->new(); $router->connect('articles', '/article', {controller => 'Article', action => 'index'}); $router->connect('edit_articles', '/article/{id}', {controller => 'Article', action => 'edit'}); $router->url_for('articles'); # => /articles $router->url_for('edit_articles', {id => 3}); # => /articles/3/edit
Dump $router as string.
The example output is following:
home GET / blog_monthly GET /blog/{year}/{month} GET /blog/{year:\d{1,4}}/{month:\d{2}}/{day:\d\d} POST /comment GET /
Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>
Tatsuhiko Miyagawa
routes.py.
Router::Simple is inspired by routes.py.
Path::Dispatcher is similar, but so complex.
Path::Router is heavy.It depend to Moose.
HTTP::Router has many deps.It doesn't well documented.
HTTPx::Dispatcher is my old one.It does not provides OOish interface.
DeNA
Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Router::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Router::Simple
CPAN shell
perl -MCPAN -e shell install Router::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.