/
Routes.pm
75 lines (50 loc) · 1.63 KB
/
Routes.pm
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
package Piglet::Routes;
use strict;
use Router::Simple;
sub new {
my $class = shift;
bless { rs => Router::Simple->new }, $class;
}
sub connect { shift->{rs}->connect(@_) }
sub match {
my($self, $env) = @_;
my $m = $self->{rs}->match($env) or return;
# magic path_info
if (exists $m->{path_info}) {
if ($env->{PATH_INFO} =~ s!^(.*?)(/?)\Q$m->{path_info}\E$!!) {
$env->{SCRIPT_NAME} .= $1;
$env->{PATH_INFO} = $2 . $m->{path_info};
} else {
# Hmm what should it do?
}
}
return $m;
}
1;
__END__
=head1 NAME
Piglet::Routes - Define routes and register web handlers
=head1 SYNOPSIS
use Piglet::Routes;
=head1 DESCRIPTION
Piglet::Routes is a simple wrapper around L<Router::Simple> that adds
more integration with PSGI application environment.
=head1 FEATURES
=head2 Magic path_info
$r->connect('/foo/{path_info:.*}', { app => $app });
my $match = $r->match($env); # /foo/bar/baz
# $env->{SCRIPT_NAME} = "/foo"
# $env->{PATH_INFO} = "/bar/baz"
If the matched parameter contains the special key C<path_info>, it
rewrites C<SCRIPT_NAME> and C<PATH_INFO> so the delegated PSGI
application behaves like it's mounted under the path, similarly to
L<Plack::App::URLMap>.
Note that in the example, route is defined as C</foo/{path_info:.*}>
and B<NOT> C</foo{path_info:.*}>. The latter also works but it would
also match with C</foobar/baz> which would result in an unwanted
behavior.
Inspired by routes.rb's magic path_info
handling. L<http://routes.groovie.org/setting_up.html#magic-path-info>
=head1 SEE ALSO
L<Piglet> L<Router::Simple>
=cut