fixed query parameter access
[catagits/CatalystX-Declare.git] / t / lib / TestApp / Controller / Foo.pm
CommitLineData
9c11a562 1use CatalystX::Declare;
918fb36e 2
dd988d3d 3namespace TestApp;
a1dd1788 4
dd988d3d 5controller ::Controller::Foo with ::TestRole {
856ac9a7 6
7 use constant MyActionNo => 'TestApp::Try::Aliasing::MyActionNo';
a1dd1788 8
856ac9a7 9 class ::Messenger {
a1dd1788 10
856ac9a7 11 has message => (is => 'rw');
12
13 method format { uc $self->message }
a1dd1788 14 }
a1dd1788 15
856ac9a7 16 role MyActionYes {
17 around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? $self->$orig(@args) : undef }
18 }
918fb36e 19
856ac9a7 20 role TestApp::Try::Aliasing::MyActionNo {
21 around match (@args) { $ENV{TESTAPP_ACTIONROLE} ? undef : $self->$orig(@args) }
22 }
23
24 class TestApp::Action::Page extends Catalyst::Action {
25
26 around execute ($controller, $ctx, @args) {
27 my $page = $ctx->request->params->{page} || 1;
accfac7d 28 $ctx->stash(page => $page);
29 return $self->$orig($controller, $ctx, @args);
856ac9a7 30 }
31 }
918fb36e 32
a0ebba1d 33 #
34 # look, a Moose!
35 #
918fb36e 36
a0ebba1d 37 has title => (
38 is => 'ro',
39 isa => 'Str',
40 default => 'TestApp',
41 );
918fb36e 42
918fb36e 43
a0ebba1d 44 #
45 # normal methods are very useful too
46 #
918fb36e 47
a0ebba1d 48 method welcome_message { sprintf 'Welcome to %s!', $self->title }
918fb36e 49
a0ebba1d 50 method greet (Str $name) { "Hello, $name!" }
918fb36e 51
918fb36e 52
a0ebba1d 53 #
54 # the simple stuff
55 #
56
57 action base under '/base' as 'foo';
58
59 action root under base as '' is final {
60 $ctx->response->body( $self->welcome_message );
61 }
62
63
64 #
65 # with arguments
66 #
67
68 action with_args under base;
69
70 action hello (Str $name) under with_args is final {
71 $ctx->response->body($self->greet(ucfirst $name));
72 }
73
74 action at_end (Int $x, Int $y) under with_args is final {
75 $ctx->response->body( $x * $y );
76 }
77
78 action in_the_middle (Int $x, Int $y) under with_args {
79 $ctx->stash(result => $x * $y);
80 }
81 action end_of_the_middle under in_the_middle is final {
82 $ctx->response->body($ctx->stash->{result} * 2);
83 }
84
85 action all_the_way (Int $x) under with_args as '' {
86 $ctx->stash(x => $x);
87 }
88 action through_the_sky (Int $y) under all_the_way as '' {
89 $ctx->stash(y => $y);
90 }
91 action and_beyond (@rest) under through_the_sky as fhtagn is final {
92 $ctx->response->body(join ', ',
93 $ctx->stash->{x},
94 $ctx->stash->{y},
95 @rest,
96 );
97 }
98
99
100 #
101 # under is also a valid keyword
102 #
103
104 under base action under_base as under;
105
106 under under_base as '' action even_more_under (Int $i) is final {
107 $ctx->response->body("under $i");
108 }
109
110
111 #
112 # too many words? go comma go!
113 #
114
115 action comma_base, as '', under base;
116
117 under comma_base, is final, action comma ($str), as ',comma' {
118 $ctx->response->body($str);
119 }
120
e10b92dd 121
122 #
123 # subnamespacing
124 #
125
126 action lower under base;
127
128 under lower {
129
130 action down;
131
132 under down {
133
134 action the;
135
136 under the {
137
138 action stream is final {
3187b9aa 139 $ctx->response->body($ctx->action->reverse);
e10b92dd 140 }
eb97acbb 141
142 action param (Int $x) {
143 $ctx->stash(param_x => $x);
144 }
145
146 under param {
147
148 final action road (Int $y) {
149 $ctx->response->body($ctx->stash->{param_x} + $y);
150 }
151 }
e10b92dd 152 }
153 }
154 }
155
a1dd1788 156
157 #
158 # action roles
159 #
160
161 action with_role_yes
162 is final
163 as with_role
164 under base
165 with MyActionYes
166 { $ctx->res->body('YES') };
167
168 action with_role_no
169 is final
170 as with_role
171 under base
172 with MyActionNo
173 { $ctx->res->body('NO') };
174
175
176 #
177 # action classes
178 #
179
180 action book (Str $title) under base {
181 $ctx->stash(title => $title);
182 }
183
accfac7d 184 action view (Str $format) under book isa Page is final {
a1dd1788 185 $ctx->response->body(
186 sprintf 'Page %d of "%s" as %s',
accfac7d 187 $ctx->stash->{page},
a1dd1788 188 $ctx->stash->{title},
189 uc($format),
190 );
191 }
2dde75e7 192
193
194 #
195 # using final as syntax element
196 #
197
198 action final_base as 'finals' under base;
199
200 final action in_front under final_base { $ctx->response->body($ctx->action->reverse) }
201
202 under final_base, final action final_middle { $ctx->response->body($ctx->action->reverse) }
203
204 action final_at_end, final under final_base { $ctx->response->body($ctx->action->reverse) }
aae7ad1f 205
206
207 #
208 # privates
209 #
210
211 action not_really_here is private { $ctx->stash(foo => 23) }
212
213 action expose_not_really_here under base is final {
214 $ctx->forward('not_really_here');
215 $ctx->response->body($ctx->stash->{foo});
216 }
64baeca0 217
218
219 #
220 # chain target specified via action
221 #
222
223 action pointed <- base ($what) is final { $ctx->response->body("Your $what is pointed!") }
856ac9a7 224
225
226 #
227 # targets for action modifiers
228 #
229
230 action modifier_target under base is final { $ctx->response->body($ctx->action->reverse) }
231
232 action surrounded_target under base is final {
233 $ctx->response->body(join ' ', $ctx->action->reverse, $ctx->response->body || ());
234 }
235
236
237 #
238 # inline classes
239 #
240
241 final action inline_class under base {
242 $ctx->response->body( TestApp::Controller::Foo::Messenger->new(message => 'Hello')->format );
243 }
5fb5cef1 244
245
246 #
247 # validation test
248 #
249
250 final action wants_integer (Int $x) under base {
251 $ctx->response->body($x);
252 }
392e5076 253
ed4a2203 254 final action wants_integer_fail (Any $x) as 'wants_integer' under base {
255 $ctx->response->body('no integer');
256 }
257
918fb36e 258}
259