Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
155 lines (89 sloc) 3.56 KB
=head1 NAME
Routes::Tiny - Routes
my $routes = Routes::Tiny->new;
# Constraints
$routes->add_route('/articles/:id', constraints => {id => qr/\d+/});
# Optional placeholders
# Defaults
defaults => {controller => 'bar', action => 'foo'});
# Grouping (matches 'hello-bar')
# Globbing (matches 'photos/foo/bar/baz')
# Path building
$routes->add_route('/:foo/:bar', name => 'default');
$routes->build_path('default', foo => 'hello', bar => 'world');
# Matching
my $match = $routes->match('/hello/world');
my $params_hashref = $match->params;
# Matching with method
my $match = $routes->match('/hello/world', method => 'GET');
L<Routes::Tiny> is a lightweight routes implementation.
L<Routes::Tiny> aims to be easy to use in any web framework.
=head2 C<Constraints>
$routes->add_route('/articles/:id', constraints => {id => qr/\d+/});
$match = $routes->match('/articles/1'); # Routes::Tiny::Match object
$match = $routes->match('/article/foo'); # undef
It is possible to specify a constraint that a placeholder must match using a
normal Perl regular expression.
=head2 C<Optional placeholders>
$routes->add_route('/admin/:service(/:action)?', defaults => {action => 'list'});
my $match = $routes->match('/admin/foo');
# $m->params is {service => 'foo', action => 'list'}
It is possible to specify an optional placeholder with a default value.
=head2 C<Grouping>
$match = $routes->match('/hello-bar');
# $match->params is {foo => 'hello'}
It is possible to create a placeholder that doesn't occupy all the space between
=head2 C<Globbing>
$match = $routes->match('photos/foo/bar/baz');
# $match->params is {other => 'foo/bar/baz'}
$match = $routes->match('books/some/section/last-words-a-memoir');
# $match->params is {section => 'some/section', title => 'last-words-a-memoir'}
$match = $routes->match('zoo/woo/foo/bar/baz');
# $match->params is {a => 'zoo/woo', b => 'bar/baz'}
It is possible to specify a globbing placeholder.
=head2 C<Path building>
$routes->add_route('/articles/:id', name => 'article');
$path = $routes->build_path('article', id => 123);
# $path is '/articles/123'
It is possible to reconstruct a path from route's name and parameters.
=head2 C<Trailing slash issue>
Trailing slash is important. Maybe this will be changed in the future.
# is different from
=head1 METHODS
=head2 C<new>
my $routes = Routes::Tiny->new;
=head2 C<add_route>
Add a new route.
=head2 C<match>
Match against a path.
=head2 C<build_path>
$pattern->build_path('name', {foo => 'bar'});
Build path from a given name and params.
=head2 Repository
=head1 AUTHOR
Viacheslav Tykhanovskyi, C<>.
Copyright (C) 2011, Viacheslav Tykhanovskyi
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.