line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Kelp::Module::Routes; |
2
|
|
|
|
|
|
|
|
3
|
31
|
|
|
31
|
|
16669
|
use Kelp::Base 'Kelp::Module'; |
|
31
|
|
|
|
|
72
|
|
|
31
|
|
|
|
|
180
|
|
4
|
31
|
|
|
31
|
|
2193
|
use Plack::Util; |
|
31
|
|
|
|
|
75
|
|
|
31
|
|
|
|
|
7109
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
my $DEFAULT_ROUTER = 'Kelp::Routes'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub build { |
9
|
40
|
|
|
40
|
1
|
131
|
my ( $self, %args ) = @_; |
10
|
|
|
|
|
|
|
|
11
|
40
|
|
66
|
|
|
242
|
my $router = delete($args{router}) // ('+' . $DEFAULT_ROUTER); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# A module name with a leading + indicates it's already fully |
14
|
|
|
|
|
|
|
# qualified (i.e., it does not need the Kelp::Routes:: prefix). |
15
|
40
|
100
|
|
|
|
1289
|
my $prefix = $router =~ s/^\+// ? undef : $DEFAULT_ROUTER; |
16
|
|
|
|
|
|
|
|
17
|
40
|
|
|
|
|
151
|
my $router_class = Plack::Util::load_class( $router, $prefix ); |
18
|
40
|
|
|
|
|
460
|
my $r = $router_class->new( %args ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Register two methods: |
21
|
|
|
|
|
|
|
# * routes - contains the routes instance |
22
|
|
|
|
|
|
|
# * add_route - a shortcut to the 'add' method |
23
|
|
|
|
|
|
|
$self->register( |
24
|
|
|
|
|
|
|
routes => $r, |
25
|
|
|
|
|
|
|
add_route => sub { |
26
|
93
|
|
|
93
|
|
387
|
my $app = shift; |
27
|
93
|
|
|
|
|
299
|
return $r->add(@_); |
28
|
|
|
|
|
|
|
} |
29
|
40
|
|
|
|
|
359
|
); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
1; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
__END__ |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=pod |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 NAME |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Kelp::Module::Routes - Default router module for Kelp |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 SYNOPSIS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# config.pl |
45
|
|
|
|
|
|
|
{ |
46
|
|
|
|
|
|
|
modules => ['Routes'], # included by default |
47
|
|
|
|
|
|
|
modules_init => { |
48
|
|
|
|
|
|
|
Routes => { |
49
|
|
|
|
|
|
|
base => 'MyApp' |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# lib/MyApp.pm |
55
|
|
|
|
|
|
|
sub build { |
56
|
|
|
|
|
|
|
my $self = shift; |
57
|
|
|
|
|
|
|
mt $self->add_route( '/', 'home' ); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 DESCRIPTION |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This module and L<Kelp::Module::Config> are automatically loaded into each Kelp |
64
|
|
|
|
|
|
|
application. It initializes the routing table for the web application. |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 REGISTERED METHODS |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
This module registers the following methods into the underlying app: |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 routes |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
An instance to L<Kelp::Routes>, or whichever router was specified in the |
73
|
|
|
|
|
|
|
configuration. |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# lib/MyApp.pm |
76
|
|
|
|
|
|
|
sub build { |
77
|
|
|
|
|
|
|
my $self = shift; |
78
|
|
|
|
|
|
|
$self->routes->add( '/', 'home' ); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 add_route |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
A shortcut to the L<Kelp::Routes/add> method. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 CONFIGURATION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The configuration for this module contains the following keys: |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head3 router |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
The router class to use. The default value is C<Kelp::Routes>, but any other |
92
|
|
|
|
|
|
|
class can be specified. A normal string will be considered a subclass of |
93
|
|
|
|
|
|
|
C<Kelp::Routes>, for example: |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
router => 'Custom' |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
will look for C<Kelp::Routes::Custom>. To specify a fully qualified class, |
98
|
|
|
|
|
|
|
prefix it with a plus sign. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
router => '+My::Special::Router |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
See L<Kelp::Routes::Controller> for a router class that reblesses the |
103
|
|
|
|
|
|
|
application instance. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head3 base |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Specifies the base class of each route. This saves a lot of typing when writing |
108
|
|
|
|
|
|
|
the routes definitions. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
base => 'MyApp' |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Now when defining a route you can only type C<myroute> to denote |
113
|
|
|
|
|
|
|
C<MyApp::myroute>. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |