forked from vti/routes-tiny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
134 lines (89 loc) · 3.69 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
NAME
Routes::Tiny - Routes
SYNOPSIS
my $routes = Routes::Tiny->new;
# Constraints
$routes->add_route('/articles/:id', constraints => {id => qr/\d+/});
# Optional placeholders
$routes->add_route('/archive/:year/(:month)?');
# Defaults
$routes->add_route('/articles/:id',
defaults => {controller => 'bar', action => 'foo'});
# Grouping (matches 'hello-bar')
$routes->add_route('/(:foo)-bar');
# Globbing (matches 'photos/foo/bar/baz')
$routes->add_route('/photos/*other');
# 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');
DESCRIPTION
Routes::Tiny is a lightweight routes implementation.
Routes::Tiny aims to be easy to use in any web framework.
FEATURES
"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.
"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.
"Grouping"
$routes->add_route('/(:foo)-bar');
$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 slashes.
"Globbing"
$routes->add_route('/photos/*other');
$routes->add_route('/books/*section/:title');
$routes->add_route('/*a/foo/*b');
$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.
"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.
WARNINGS
"Trailing slash issue"
Trailing slash is important. Maybe this will be changed in the future.
$routes->add_route('/articles');
# is different from
$routes->add_route('/articles/');
METHODS
"new"
my $routes = Routes::Tiny->new;
"add_route"
$routes->add_route('/:service/:action');
Add a new route.
"match"
$routes->match('/hello/world');
Match against a path.
"build_path"
$pattern->build_path('name', {foo => 'bar'});
Build path from a given name and params.
DEVELOPMENT
Repository
http://github.com/vti/routes-tiny
CREDITS
Sergey Zasenko (und3f)
AUTHOR
Viacheslav Tykhanovskyi, "[email protected]".
COPYRIGHT AND LICENSE
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.