Skip to content

Latest commit

 

History

History
210 lines (130 loc) · 6.4 KB

README.mkdn

File metadata and controls

210 lines (130 loc) · 6.4 KB

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)) {
        return "MyApp::C::$p->{controller}"->can($p->{action})->($env, $p);
    } 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 is PSGI friendly.

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', args => { 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 'splat'.

'{year}' notation

$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 be captured as 'args'.

'{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', args => { 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.

METHODS

  • my $router = Router::Simple->new();

Creates a new instance of Router::Simple.

  • $router->connect([$name, ] $pattern, %destination[, %options])

Adds a new rule to $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'} );

%destination will use by match method.

You can specify some optional things to %options. The current version supports 'method', 'host', and 'on_match'.

- method

'method' is an ArrayRef[String] or String that matches __REQUEST_METHOD__ in $req.

- host

'host' is a String or Regexp that matches __HTTP_HOST__ in $req.

- on_match

    $r->connect(
        '/{controller}/{action}/{id}',
        +{},
        +{
            on_match => sub {
                my ($req, $match) = @_;
                $match->{referer} = $req->{HTTP_REFERER};
                return 1;
            }
        }
    );

A function that evaluates the request. Its signature must be ($environ, $match_dict) => bool. It should return true if the match is successful or false otherwise. The first arg is $req; the second is the routing variables that would be returned if the match succeeds. The function can modify $match_dict in place to affect which variables are returned. This allows a wide range of transformations.
  • $router->submapper($path, [%dest, [%opt]])

    $router->submapper('/entry/, {controller => 'Entry'})

This method is shorthand for creating new instance of Router::Simple::Submapper.

The arguments will be passed to Router::Simple::SubMapper->new(%args).

  • $router->match($req|$path)

Matches a URL against one of the contained routes.

$req is a PSGI $env or a plain string.

This method returns a plain hashref.

If you are using the +{ controller => 'Blog', action => 'daily' } style, then the value returned will look like:

{
    controller => 'Blog',
    action     => 'daily',
    year => 2010, month => '03', day => '04',
}

This will return undef if no valid match is found.

  • $router->url_for($anchor, %opts)

Generate a path string from the rule named $anchor.

You must pass each parameter in %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
  • $router->as_string()

Dumps $router as string.

Example output:

home         GET  /
blog_monthly GET  /blog/{year}/{month}
             GET  /blog/{year:\d{1,4}}/{month:\d{2}}/{day:\d\d}
             POST /comment
             GET  /

AUTHOR

Tokuhiro Matsuno

THANKS TO

Tatsuhiko Miyagawa

Shawn M Moore

L<routes.py|http://routes.groovie.org/>.

SEE ALSO

Router::Simple is inspired by L<routes.py|http://routes.groovie.org/>.

Path::Dispatcher is similar, but so complex.

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

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

HTTPx::Dispatcher is my old one. It does not provide an OOish 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.