-
Notifications
You must be signed in to change notification settings - Fork 1
/
README
256 lines (187 loc) · 6.43 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
NAME
CatalystX::Controller::Sugar - Sugar for Catalyst controller
VERSION
0.0702
DESCRIPTION
This module is written to simplify the way controllers are written. I
personally think that shifting off $c and $self in every action is
tidious. I also wanted a simpler API to created chained actions, since I
rarely use any other actions - except of "private".
SYNOPSIS
package MyApp::Controller::Root;
use CatalystX::Controller::Sugar;
__PACKAGE__->config->{'namespace'} = q();
# Private action
private authenticate => sub {
c->user_exists and return 1;
};
# Chain /
chain sub {
report debug => 'Someone tries to access %s', c->action;
};
# Endpioint /*
chain '' => sub {
res->body('not found');
};
# Endpoint /login
chain login => {
get => sub {}, # show template
post => sub {
forward 'authenticate' and go '';
},
};
# Chain /user/[id]/*
chain user => ['id'], sub {
stash user => c->model('DB::User')->find($_[0]);
};
# Endpoint /user/[id]/view/*
chain 'user:1' => view => sub {
res->body(
sprintf 'Person is called: %s', stash->{'user'}->name
);
};
Same with standard Catalyst syntax
package MyApp::Controller::Root;
use Moose;
BEGIN { extends 'Catalyst::Controller' }
__PACKAGE__->config->{'namespace'} = q();
# Private action
sub authenticate :Private {
my($self, $c) = @_;
$c->user_exists and return 1;
}
# Chain /
sub root :Chained("/") PathPart("") CaptureArgs(0) {
my($self, $c) = @_;
$c->log->debug(sprintf 'Someone tries to access %s', $c->action);
}
# Endpioint /*
sub default :Chained("/root") PathPart("") Args {
my($self, $c) = @_;
$c->res->body('not found');
}
# Endpoint /login
sub login :Chained("/root") PathPart Args {
my($self, $c) = @_;
if(lc $c->req->method eq 'get') {
return; # show template
}
elsif(lc $c->req->method eq 'post') {
$c->forward('authenticate') and go('');
}
}
# Chain /user/[id]/*
sub user :Chained("/root") PathPart CaptureArgs(1) {
my($self, $c, $id) = @_;
$c->stash->{'id'} = $id; # alternative to captured('id');
$c->stash->{'user'} = $c->model('DB::User')->find($id);
}
# Endpoint /user/[id]/view/*
sub user_view :Chained("/user") PathPart('view') Args {
my($self, $c) = @_;
$c->res->body(sprintf 'Person is called: %s', $c->stash->{'user'}->name);
}
NOTE
$self and $c is not part of the argument list inside a chain() or
private() action. $c is acquired by calling c(), and $self is available
by calling controller().
EXPORTED FUNCTIONS
chain
1. chain sub { };
2. chain $PathPart => sub { };
3. chain $PathPart => $Int, sub { };
4. chain $PathPart => \@CaptureArgs, sub { };
5. chain $Chained => $PathPart => sub { };
6. chain $Chained => $PathPart => $Int, sub { };
7. chain $Chained => $PathPart => \@CaptureArgs, sub { };
8. chain ..., \%method_map;
Same as:
1. sub root : Chained('/') PathPart('') CaptureArgs(0) { }
2. sub $PathPart : Chained('/root') Args { }
3. sub $PathPart : Chained('/root') Args($Int) { }
4. sub $PathPart : Chained('/root') CaptureArgs($Int) { }
5. sub $PathPart : Chained($Chained) Args { }
6. sub $PathPart : Chained($Chained) Args($Int) { }
7. sub $PathPart : Chained($Chained) CaptureArgs($Int) { }
8. Special case: See below
@CaptureArgs is a list of names of the captured arguments, which can be
retrieved using captured().
$Int is a number of Args to capture at the endpoint of a chain. These
cannot be aquired using captured(), but is instead available in @_.
%method_map can be used if you want to dispatch to a specific method,
for a certain HTTP method: (The HTTP method is in lowercase)
%method_map = (
post => sub { ... },
get => sub { ... },
delete => sub { ... },
default => sub { ... },
#...
);
private
private $name => sub {};
Same as:
sub $name :Private {}
forward
@Any = forward $action;
@Any = forward $action, \@arguments;
See Catalyst::forward().
go
go $action;
go $action, \@arguments;
See Catalyst::go().
c
$context_obj = c;
Returns the context object for this request, an instance of Catalyst.
controller
$controller_obj = controller;
Returns the current controller object.
req
$request_obj = req;
Returns the request object for this request, an instance of
Catalyst::Request.
res
$response_obj = res;
Returns the response object for this request, an instance of
Catalyst::Response.
captured
$value = captured($name);
Retrieve data captured in a chain, using the names set with chain().
chain '/' => 'user' => ['id'], sub {
res->body( captured('id') );
};
stash
$value = stash $key;
$hash_ref = stash $key => $value, ...;
$hash_ref = stash;
Set/get data from the stash. The $hash_ref is a reference to what the
stash is holding.
This will be the same as:
$c->stash->{$key} = $value;
session
$value = session $key;
$hash_ref == session $key => $value;
$hash_ref == session;
Set/get data from the session. The $hash_ref is a reference to what the
session is holding.
This function will only work if a session module/plugin is loaded into
Catalyst.
report
report $level, $format, @args;
Almost the same as:
$c->log->$level(sprintf $format, @args);
But undef values from @args are turned into "__UNDEF__", and objects
and/or datastructructures are flatten, using Data::Dumper.
METHODS
init_meta
See Moose::Exporter.
BUGS
Please report any bugs or feature requests to
"bug-catalystx-controller-sugar at rt.cpan.org". I will be notified, and
then you'll automatically be notified of progress on your bug as I make
changes.
COPYRIGHT & LICENSE
Copyright 2007 Jan Henning Thorsen, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
AUTHOR
Jan Henning Thorsen, "<jhthorsen at cpan.org>"