Commit | Line | Data |
fc7ec1d9 |
1 | =head1 NAME |
2 | |
3 | Catalyst::Manual::Intro - Introduction to Catalyst |
4 | |
5 | =head1 DESCRIPTION |
6 | |
129cfe74 |
7 | This is a brief overview of why and how to use Catalyst. It explains how |
8 | Catalyst works and shows how to get a simple application up and running quickly. |
fc7ec1d9 |
9 | |
10 | =head2 What is Catalyst? |
11 | |
129cfe74 |
12 | Catalyst is an elegant web application framework, extremely flexible yet |
13 | extremely simple. It's similar to Ruby on Rails, Spring (Java) and L<Maypole>, |
14 | upon which it was originally based. |
fc7ec1d9 |
15 | |
16 | =head3 MVC |
17 | |
129cfe74 |
18 | Catalyst follows the Model-View-Controller (MVC) design pattern, allowing you to |
19 | easily separate concerns, like content, presentation, and flow control, into |
20 | separate modules. This separation allows you to modify code that handles one |
21 | concern without affecting code that handles the others. Catalyst promotes the |
22 | re-use of existing Perl modules that already handle common web application |
23 | concerns well. |
fc7ec1d9 |
24 | |
129cfe74 |
25 | Here's how the M, V, and C map to those concerns, with examples of well-known |
26 | Perl modules you may want to use for each. |
fc7ec1d9 |
27 | |
28 | =over 4 |
29 | |
4a6895ce |
30 | =item * B<Model> |
fc7ec1d9 |
31 | |
32 | Access and modify content (data). L<Class::DBI>, L<Plucene>, L<Net::LDAP>... |
33 | |
4a6895ce |
34 | =item * B<View> |
fc7ec1d9 |
35 | |
129cfe74 |
36 | Present content to the user. L<Template Toolkit|Template>, L<Mason|HTML::Mason>, |
37 | L<HTML::Template>... |
fc7ec1d9 |
38 | |
4a6895ce |
39 | =item * B<Controller> |
fc7ec1d9 |
40 | |
129cfe74 |
41 | Control the whole request phase, check parameters, dispatch actions, flow |
42 | control. Catalyst! |
fc7ec1d9 |
43 | |
44 | =back |
45 | |
26e73131 |
46 | If you're unfamiliar with MVC and design patterns, you may want to check |
47 | out the original book on the subject, I<Design Patterns>, by Gamma, |
48 | Helm, Johnson, and Vlissides, also known as the Gang of Four (GoF). You |
49 | can also just Google it. Many, many web application frameworks are |
50 | based on MVC, including all those listed above. |
fc7ec1d9 |
51 | |
52 | =head3 Flexibility |
53 | |
129cfe74 |
54 | Catalyst is much more flexible than many other frameworks. We'll talk more about |
55 | this later, but rest assured you can use your favorite Perl modules with |
56 | Catalyst. |
fc7ec1d9 |
57 | |
58 | =over 4 |
59 | |
72d9bfc7 |
60 | =item * B<Multiple Models, Views, and Controllers> |
fc7ec1d9 |
61 | |
129cfe74 |
62 | To build a Catalyst application, you handle each type of concern inside special |
63 | modules called L</Components>. Often this code will be very simple, just calling |
64 | out to Perl modules like those listed above under L</MVC>. Catalyst handles |
65 | these components in a very flexible way. Use as many Models, Views, and |
66 | Controllers as you like, using as many different Perl modules as you like, all |
67 | in the same application. Want to manipulate multiple databases, and retrieve |
68 | some data via LDAP? No problem. Want to present data from the same Model using |
69 | L<Template Toolkit|Template> and L<PDF::Template>? Easy. |
fc7ec1d9 |
70 | |
cda8d1ac |
71 | =item * B<Reuseable Components> |
fc7ec1d9 |
72 | |
129cfe74 |
73 | Not only does Catalyst promote the re-use of already existing Perl modules, it |
74 | also allows you to re-use your Catalyst components in multiple Catalyst |
75 | applications. |
fc7ec1d9 |
76 | |
4a6895ce |
77 | =item * B<Unrestrained URL-to-Action Dispatching> |
fc7ec1d9 |
78 | |
129cfe74 |
79 | Catalyst allows you to dispatch any URLs to any application L<Actions>, even |
80 | through regular expressions! Unlike most other frameworks, it doesn't require |
81 | mod_rewrite or class and method names in URLs. |
fc7ec1d9 |
82 | |
83 | With Catalyst you register your actions and address them directly. For example: |
84 | |
e3dc9d78 |
85 | sub hello : Global { |
fc7ec1d9 |
86 | my ( $self, $context ) = @_; |
66f6e959 |
87 | $context->response->body('Hello World!'); |
5a8ed4fe |
88 | } |
fc7ec1d9 |
89 | |
90 | Now http://localhost:3000/hello prints "Hello World!". |
91 | |
4a6895ce |
92 | =item * B<Support for CGI, mod_perl, Apache::Request> |
fc7ec1d9 |
93 | |
94 | Use L<Catalyst::Engine::Apache> or L<Catalyst::Engine::CGI>. |
95 | |
96 | =back |
97 | |
98 | =head3 Simplicity |
99 | |
129cfe74 |
100 | The best part is that Catalyst implements all this flexibility in a very simple |
101 | way. |
fc7ec1d9 |
102 | |
6f4e1683 |
103 | =over 4 |
104 | |
4a6895ce |
105 | =item * B<Building Block Interface> |
fc7ec1d9 |
106 | |
129cfe74 |
107 | Components interoperate very smoothly. For example, Catalyst automatically makes |
108 | a L<Context> object available to every component. Via the context, you can |
109 | access the request object, share data between components, and control the flow |
110 | of your application. Building a Catalyst application feels a lot like snapping |
111 | together toy building blocks, and everything just works. |
fc7ec1d9 |
112 | |
4a6895ce |
113 | =item * B<Component Auto-Discovery> |
fc7ec1d9 |
114 | |
129cfe74 |
115 | No need to C<use> all of your components. Catalyst automatically finds and loads |
116 | them. |
fc7ec1d9 |
117 | |
4a6895ce |
118 | =item * B<Pre-Built Components for Popular Modules> |
fc7ec1d9 |
119 | |
129cfe74 |
120 | See L<Catalyst::Model::CDBI> for L<Class::DBI>, or L<Catalyst::View::TT> for |
121 | L<Template Toolkit|Template>. You can even get an instant web database front end |
122 | with L<Catalyst::Model::CDBI::CRUD>. |
fc7ec1d9 |
123 | |
72d9bfc7 |
124 | =item * B<Built-in Test Framework> |
fc7ec1d9 |
125 | |
129cfe74 |
126 | Catalyst comes with a built-in, lightweight http server and test framework, |
127 | making it easy to test applications from the command line. |
fc7ec1d9 |
128 | |
4a6895ce |
129 | =item * B<Helper Scripts> |
fc7ec1d9 |
130 | |
129cfe74 |
131 | Catalyst provides helper scripts to quickly generate running starter code for |
132 | components and unit tests. |
fc7ec1d9 |
133 | |
6f4e1683 |
134 | =back |
135 | |
fc7ec1d9 |
136 | =head2 Quickstart |
137 | |
129cfe74 |
138 | Here's how to install Catalyst and get a simple application up and running, |
139 | using the helper scripts described above. |
fc7ec1d9 |
140 | |
141 | =head3 Install |
142 | |
143 | $ perl -MCPAN -e 'install Bundle::Catalyst' |
144 | |
145 | =head3 Setup |
146 | |
2feb6632 |
147 | $ catalyst.pl MyApp |
b33ed88c |
148 | # output omitted |
2feb6632 |
149 | $ cd MyApp |
ac4a0ae0 |
150 | $ script/myapp_create.pl controller Library::Login |
fc7ec1d9 |
151 | |
152 | =head3 Run |
153 | |
b33ed88c |
154 | $ script/myapp_server.pl |
fc7ec1d9 |
155 | |
129cfe74 |
156 | Now visit these locations with your favorite browser or user agent to see |
157 | Catalyst in action: |
fc7ec1d9 |
158 | |
159 | =over 4 |
160 | |
161 | =item http://localhost:3000/ |
162 | |
ac4a0ae0 |
163 | =item http://localhost:3000/library/login/ |
fc7ec1d9 |
164 | |
165 | =back |
166 | |
167 | Dead easy! |
168 | |
169 | =head2 How It Works |
170 | |
129cfe74 |
171 | Let's see how Catalyst works, by taking a closer look at the components and |
172 | other parts of a Catalyst application. |
fc7ec1d9 |
173 | |
174 | =head3 Application Class |
175 | |
129cfe74 |
176 | In addition to the Model, View, and Controller components, there's a single |
177 | class that represents your application itself. This is where you configure your |
178 | application, load plugins, define application-wide actions, and extend Catalyst. |
fc7ec1d9 |
179 | |
180 | package MyApp; |
181 | |
182 | use strict; |
183 | use Catalyst qw/-Debug/; |
184 | |
185 | MyApp->config( |
186 | name => 'My Application', |
fc7ec1d9 |
187 | |
b33ed88c |
188 | # You can put anything else you want in here: |
189 | my_configuration_variable => 'something', |
fc7ec1d9 |
190 | ); |
191 | |
5a8ed4fe |
192 | sub default : Private { |
fc7ec1d9 |
193 | my ( $self, $context ) = @_; |
66f6e959 |
194 | $context->response->body('Catalyst rockz!'); |
5a8ed4fe |
195 | } |
fc7ec1d9 |
196 | |
197 | 1; |
198 | |
66f6e959 |
199 | For most applications, Catalyst requires you to define only one config |
200 | parameter: |
fc7ec1d9 |
201 | |
202 | =over 4 |
203 | |
4a6895ce |
204 | =item * B<name> |
fc7ec1d9 |
205 | |
206 | Name of your application. |
207 | |
fc7ec1d9 |
208 | =back |
209 | |
66f6e959 |
210 | Optionally, you can specify a B<root> parameter for templates and static data. |
211 | If omitted, Catalyst will try to auto-detect the directory's location. You |
212 | can define as many parameters as you want for plugins or whatever you |
213 | need. You can access them anywhere in your application |
214 | via C<$context-E<gt>config-E<gt>{$param_name}>. |
fc7ec1d9 |
215 | |
216 | =head3 Context |
217 | |
129cfe74 |
218 | Catalyst automatically blesses a Context object into your application class and |
219 | makes it available everywhere in your application. Use the Context to directly |
220 | interact with Catalyst and glue your L<Components> together. For example, if you |
221 | need to use the Context from within a Template Toolkit template, it's already |
222 | there: |
c42f5bbf |
223 | |
224 | <h1>Welcome to [% c.config.name %]!</h1> |
fc7ec1d9 |
225 | |
129cfe74 |
226 | As illustrated earlier in our URL-to-Action dispatching example, the Context is |
227 | always the second method parameter, behind the Component object reference or |
228 | class name itself. Previously we called it C<$context> for clarity, but most |
229 | Catalyst developers just call it C<$c>: |
fc7ec1d9 |
230 | |
e3dc9d78 |
231 | sub hello : Global { |
fc7ec1d9 |
232 | my ( $self, $c ) = @_; |
66f6e959 |
233 | $c->res->body('Hello World!'); |
5a8ed4fe |
234 | } |
fc7ec1d9 |
235 | |
236 | The Context contains several important objects: |
237 | |
238 | =over 4 |
239 | |
240 | =item * L<Catalyst::Request> |
241 | |
242 | $c->request |
243 | $c->req # alias |
244 | |
129cfe74 |
245 | The request object contains all kinds of request-specific information, like |
246 | query parameters, cookies, uploads, headers, and more. |
fc7ec1d9 |
247 | |
248 | $c->req->params->{foo}; |
249 | $c->req->cookies->{sessionid}; |
250 | $c->req->headers->content_type; |
251 | $c->req->base; |
252 | |
afdca3a3 |
253 | =item * L<Catalyst::Response> |
fc7ec1d9 |
254 | |
255 | $c->response |
256 | $c->res # alias |
257 | |
129cfe74 |
258 | The response is like the request, but contains just response-specific |
259 | information. |
fc7ec1d9 |
260 | |
66f6e959 |
261 | $c->res->body('Hello World'); |
fc7ec1d9 |
262 | $c->res->status(404); |
263 | $c->res->redirect('http://oook.de'); |
264 | |
265 | =item * L<Catalyst::Config> |
266 | |
267 | $c->config |
268 | |
269 | $c->config->root; |
270 | $c->config->name; |
271 | |
272 | =item * L<Catalyst::Log> |
273 | |
274 | $c->log |
275 | |
276 | $c->log->debug('Something happened'); |
277 | $c->log->info('Something you should know'); |
278 | |
4a6895ce |
279 | =item * B<Stash> |
fc7ec1d9 |
280 | |
281 | $c->stash |
282 | |
283 | $c->stash->{foo} = 'bar'; |
284 | |
285 | =back |
286 | |
129cfe74 |
287 | The last of these, the stash, is a universal hash for sharing data among |
288 | application components. For an example, we return to our 'hello' action: |
fc7ec1d9 |
289 | |
e3dc9d78 |
290 | sub hello : Global { |
5a8ed4fe |
291 | my ( $self, $c ) = @_; |
292 | $c->stash->{message} = 'Hello World!'; |
4c6807d2 |
293 | $c->forward('show_message'); |
5a8ed4fe |
294 | } |
fc7ec1d9 |
295 | |
4c6807d2 |
296 | sub show_message : Private { |
5a8ed4fe |
297 | my ( $self, $c ) = @_; |
66f6e959 |
298 | $c->res->body( $c->stash->{message} ); |
5a8ed4fe |
299 | } |
fc7ec1d9 |
300 | |
129cfe74 |
301 | Note that the stash should be used only for passing data in an individual |
302 | request cycle; it gets cleared at a new request. If you need to maintain more |
303 | persistent data, use a session. |
dd25a192 |
304 | |
fc7ec1d9 |
305 | =head3 Actions |
306 | |
129cfe74 |
307 | A Catalyst controller is defined by its actions. An action is a sub with a |
308 | special attribute. You've already seen some examples of actions in this |
309 | document. The URL (for example http://localhost.3000/foo/bar) consists of two |
310 | parts, the base (http://localhost:3000/ in this example) and the path (foo/bar). |
311 | Please note that the trailing slash after the hostname[:port] always belongs to |
312 | base and not to the action. |
cda8d1ac |
313 | |
314 | Catalyst supports several types of actions: |
fc7ec1d9 |
315 | |
316 | =over 4 |
317 | |
4a6895ce |
318 | =item * B<Literal> |
fc7ec1d9 |
319 | |
f29c48dd |
320 | sub bar : Path('foo/bar') { } |
fc7ec1d9 |
321 | |
322 | Matches only http://localhost:3000/foo/bar. |
323 | |
4a6895ce |
324 | =item * B<Regex> |
fc7ec1d9 |
325 | |
b33ed88c |
326 | sub bar : Regex('^item(\d+)/order(\d+)$') { } |
fc7ec1d9 |
327 | |
129cfe74 |
328 | Matches any URL that matches the pattern in the action key, e.g. |
329 | http://localhost:3000/item23/order42. The '' around the regexp is optional, but |
330 | perltidy likes it. :) |
b33ed88c |
331 | |
129cfe74 |
332 | Regex matches act globally, i.e. without reference to the namespace from which |
333 | it is called, so that a C<bar> method in the |
334 | C<MyApp::Controller::Catalog::Order::Process> namespace won't match any form of |
335 | C<bar>, C<Catalog>, C<Order>, or C<Process> unless you explicitly put this in |
66f6e959 |
336 | the regex. To achieve the above, you should consider using a C<LocalRegex> action. |
337 | |
338 | =item * B<LocalRegex> |
339 | |
340 | sub bar : LocalRegex('^widget(\d+)$') { } |
fc7ec1d9 |
341 | |
66f6e959 |
342 | LocalRegex actions act locally. If you were to use C<bar> in |
343 | C<MyApp::Controller::Catalogue>, the above example would match urls like |
344 | http://localhost:3000/catalogue/widget23. |
345 | |
346 | For both LocalRegex and Regex actions, if you use capturing parentheses to |
347 | extract values within the matching URL ("widget23" would capture "23" in the |
348 | above example), those values are available in the $c->req->snippets |
129cfe74 |
349 | array. If you want to pass arguments at the end of your URL, you must use regex |
350 | action keys. See L</URL Argument Handling> below. |
fc7ec1d9 |
351 | |
72d9bfc7 |
352 | =item * B<Top-level> |
cda8d1ac |
353 | |
354 | package MyApp; |
355 | sub foo : Global { } |
356 | |
b33ed88c |
357 | Matches http://localhost:3000/foo. The function name is mapped directly |
358 | to the application base. |
cda8d1ac |
359 | |
4a6895ce |
360 | =item * B<Namespace-Prefixed> |
fc7ec1d9 |
361 | |
2feb6632 |
362 | package MyApp::C::My::Controller; |
e3dc9d78 |
363 | sub foo : Local { } |
fc7ec1d9 |
364 | |
cda8d1ac |
365 | Matches http://localhost:3000/my/controller/foo. |
fc7ec1d9 |
366 | |
129cfe74 |
367 | This action type indicates that the matching URL must be prefixed with a |
368 | modified form of the component's class (package) name. This modified class name |
369 | excludes the parts that have a pre-defined meaning in Catalyst ("MyApp::C" in |
370 | the above example), replaces "::" with "/", and converts the name to lower case. |
371 | See L</Components> for a full explanation of the pre-defined meaning of Catalyst |
372 | component class names. |
fc7ec1d9 |
373 | |
4a6895ce |
374 | =item * B<Private> |
fc7ec1d9 |
375 | |
5a8ed4fe |
376 | sub foo : Private { } |
fc7ec1d9 |
377 | |
129cfe74 |
378 | Matches no URL, and cannot be executed by requesting a URL that corresponds to |
379 | the action key. Private actions can be executed only inside a Catalyst |
380 | application, by calling the C<forward> method: |
fc7ec1d9 |
381 | |
5a8ed4fe |
382 | $c->forward('foo'); |
fc7ec1d9 |
383 | |
129cfe74 |
384 | See L</Flow Control> for a full explanation of C<forward>. Note that, as |
fc9c8698 |
385 | discussed there, when forwarding from another component, you must use |
386 | the absolute path to the method, so that a private C<bar> method in your |
387 | C<MyApp::Controller::Catalog::Order::Process> controller must, if called |
388 | from elsewhere, be reached with |
389 | C<$c-E<gt>forward('/catalog/order/process/bar')>. |
fc7ec1d9 |
390 | |
391 | =back |
392 | |
b33ed88c |
393 | B<Note:> After seeing these examples, you probably wonder what the point |
394 | is of defining names for regex and path actions. Actually, every public |
395 | action is also a private one, so you have one unified way of addressing |
396 | components in your C<forward>s. |
cda8d1ac |
397 | |
72d9bfc7 |
398 | =head4 Built-in Private Actions |
fc7ec1d9 |
399 | |
fc9c8698 |
400 | In response to specific application states, Catalyst will automatically |
401 | call these built-in private actions in your application class: |
fc7ec1d9 |
402 | |
403 | =over 4 |
404 | |
cda8d1ac |
405 | =item * B<default : Private> |
fc7ec1d9 |
406 | |
fc9c8698 |
407 | Called when no other action matches. Could be used, for example, for |
408 | displaying a generic frontpage for the main app, or an error page for |
409 | individual controllers. |
fc7ec1d9 |
410 | |
66f6e959 |
411 | =item * B<index : Private> |
412 | |
413 | C<index> is much like C<default> except that it takes no arguments |
414 | and it is weighted slightly higher in the matching process. |
415 | |
cda8d1ac |
416 | =item * B<begin : Private> |
fc7ec1d9 |
417 | |
fc9c8698 |
418 | Called at the beginning of a request, before any matching actions are |
419 | called. |
fc7ec1d9 |
420 | |
cda8d1ac |
421 | =item * B<end : Private> |
4a6895ce |
422 | |
fc7ec1d9 |
423 | Called at the end of a request, after all matching actions are called. |
424 | |
fc9c8698 |
425 | =back |
426 | |
6b10c72b |
427 | =head4 Built-in actions in controllers/autochaining |
fc7ec1d9 |
428 | |
cda8d1ac |
429 | Package MyApp::C::Foo; |
430 | sub begin : Private { } |
5a8ed4fe |
431 | sub default : Private { } |
eff5f524 |
432 | sub auto : Private { } |
fc7ec1d9 |
433 | |
fc9c8698 |
434 | You can define built-in private actions within your controllers as |
435 | well. The actions will override the ones in less-specific controllers, |
436 | or your application class. In other words, for each of the three |
437 | built-in private actions, only one will be run in any request |
438 | cycle. Thus, if C<MyApp::C::Catalog::begin> exists, it will be run in |
439 | place of C<MyApp::begin> if you're in the C<catalog> namespace, and |
440 | C<MyApp::C::Catalog::Order::begin> would override this in turn. |
441 | |
eff5f524 |
442 | In addition to the normal built-in actions, you have a special action |
443 | for making chains, C<auto>. Such C<auto> actions will be run after any |
fc9c8698 |
444 | C<begin>, but before your action is processed. Unlike the other |
eff5f524 |
445 | built-ins, C<auto> actions I<do not> override each other; they will be |
446 | called in turn, starting with the application class and going through to |
447 | the I<most> specific class. I<This is the reverse of the order in which |
448 | the normal built-ins override each other>. |
fc9c8698 |
449 | |
450 | Here are some examples of the order in which the various built-ins |
451 | would be called: |
cda8d1ac |
452 | |
453 | =over 4 |
454 | |
fc9c8698 |
455 | =item for a request for C</foo/foo> |
cda8d1ac |
456 | |
457 | MyApp::begin |
80ef2e6d |
458 | MyApp::auto |
fc9c8698 |
459 | MyApp::C::Foo::default # in the absence of MyApp::C::Foo::Foo |
cda8d1ac |
460 | MyApp::end |
461 | |
fc9c8698 |
462 | =item for a request for C</foo/bar/foo> |
cda8d1ac |
463 | |
cda8d1ac |
464 | MyApp::C::Foo::Bar::begin |
80ef2e6d |
465 | MyApp::auto |
466 | MyApp::C::Foo::auto |
fc9c8698 |
467 | MyApp::C::Foo::Bar::auto |
468 | MyApp::C::Foo::Bar::default # for MyApp::C::Foo::Bar::foo |
cda8d1ac |
469 | MyApp::C::Foo::Bar::end |
80ef2e6d |
470 | |
471 | =back |
472 | |
fc9c8698 |
473 | The C<auto> action is also distinguished by the fact that you can break |
474 | out of the processing chain by returning 0. If an C<auto> action returns |
475 | 0, any remaining actions will be skipped, except for C<end>. So, for the |
476 | request above, if the first auto returns false, the chain would look |
477 | like this: |
80ef2e6d |
478 | |
479 | =over 4 |
480 | |
fc9c8698 |
481 | =item for a request for C</foo/bar/foo> where first C<auto> returns |
482 | false |
80ef2e6d |
483 | |
484 | MyApp::C::Foo::Bar::begin |
485 | MyApp::auto |
486 | MyApp::C::Foo::Bar::end |
cda8d1ac |
487 | |
488 | =back |
4a6895ce |
489 | |
fc9c8698 |
490 | An example of why one might use this is an authentication action: you |
491 | could set up a C<auto> action to handle authentication in your |
492 | application class (which will always be called first), and if |
493 | authentication fails, returning 0 would skip any remaining methods |
494 | for that URL. |
03805733 |
495 | |
fc9c8698 |
496 | B<Note:> Looking at it another way, C<auto> actions have to return a |
497 | true value to continue processing! You can also C<die> in the autochain |
498 | action; in that case, the request will go straight to the finalize |
499 | stage, without processing further actions. |
03805733 |
500 | |
6b10c72b |
501 | =head4 URL Path Handling |
4a6895ce |
502 | |
fc9c8698 |
503 | You can pass variable arguments as part of the URL path. In this case, |
504 | you must use regex action keys with '^' and '$' anchors, and the |
505 | arguments must be separated with forward slashes (/) in the URL. For |
506 | example, suppose you want to handle C</foo/$bar/$baz>, where C<$bar> and |
507 | C<$baz> may vary: |
4a6895ce |
508 | |
cda8d1ac |
509 | sub foo : Regex('^foo$') { my ($self, $context, $bar, $baz) = @_; } |
4a6895ce |
510 | |
fc9c8698 |
511 | But what if you also defined actions for C</foo/boo> and C</foo/boo/hoo>? |
4a6895ce |
512 | |
f29c48dd |
513 | sub boo : Path('foo/boo') { .. } |
514 | sub hoo : Path('foo/boo/hoo') { .. } |
4a6895ce |
515 | |
516 | Catalyst matches actions in most specific to least specific order: |
517 | |
518 | /foo/boo/hoo |
519 | /foo/boo |
fc9c8698 |
520 | /foo # might be /foo/bar/baz but won't be /foo/boo/hoo |
4a6895ce |
521 | |
fc9c8698 |
522 | So Catalyst would never mistakenly dispatch the first two URLs to the |
523 | '^foo$' action. |
fc7ec1d9 |
524 | |
6b10c72b |
525 | =head4 Parameter Processing |
2ef2fb0f |
526 | |
fc9c8698 |
527 | Parameters passed in the URL query string are handled with methods in |
528 | the L<Catalyst::Request> class. The C<param> method is functionally |
529 | equivalent to the C<param> method of C<CGI.pm> and can be used in |
530 | modules that require this. |
2ef2fb0f |
531 | |
532 | # http://localhost:3000/catalog/view/?category=hardware&page=3 |
533 | my $category = $c->req->param('category'); |
534 | my $current_page = $c->req->param('page') || 1; |
535 | |
536 | # multiple values for single parameter name |
537 | my @values = $c->req->param('scrolling_list'); |
538 | |
539 | # DFV requires a CGI.pm-like input hash |
540 | my $results = Data::FormValidator->check($c->req->params, \%dfv_profile); |
541 | |
fc7ec1d9 |
542 | =head3 Flow Control |
543 | |
d08ced28 |
544 | You control the application flow with the C<forward> method, which |
545 | accepts the key of an action to execute. This can be an action in the |
546 | same or another Catalyst controller, or a Class name, optionally |
547 | followed by a method name. After a C<forward>, the control flow will |
548 | return to the method from which the C<forward> was issued. |
549 | |
550 | A C<forward> is similar to a method call. The main differences are that |
551 | it wraps the call in an C<eval> to allow exception handling; it |
552 | automatically passes along the context object (C<$c> or C<$context>); |
553 | and it allows profiling of each call (displayed in the log with |
554 | debugging enabled). |
fc7ec1d9 |
555 | |
e3dc9d78 |
556 | sub hello : Global { |
5a8ed4fe |
557 | my ( $self, $c ) = @_; |
558 | $c->stash->{message} = 'Hello World!'; |
d08ced28 |
559 | $c->forward('check_message'); # $c is automatically included |
5a8ed4fe |
560 | } |
fc7ec1d9 |
561 | |
4c6807d2 |
562 | sub check_message : Private { |
5a8ed4fe |
563 | my ( $self, $c ) = @_; |
564 | return unless $c->stash->{message}; |
4c6807d2 |
565 | $c->forward('show_message'); |
5a8ed4fe |
566 | } |
fc7ec1d9 |
567 | |
4c6807d2 |
568 | sub show_message : Private { |
5a8ed4fe |
569 | my ( $self, $c ) = @_; |
66f6e959 |
570 | $c->res->body( $c->stash->{message} ); |
5a8ed4fe |
571 | } |
3323f920 |
572 | |
d08ced28 |
573 | A C<forward> does not create a new request, so your request |
574 | object (C<$c-E<gt>req>) will remain unchanged. This is a |
575 | key difference between using C<forward> and issuing a |
576 | redirect. |
3323f920 |
577 | |
d08ced28 |
578 | You can pass new arguments to a C<forward> by adding them |
579 | in an anonymous array. In this case C<$c-E<gt>req-E<gt>args> |
580 | will be changed for the duration of the C<forward> only; upon |
581 | return, the original value of C<$c-E<gt>req-E<gt>args> will |
582 | be reset. |
3323f920 |
583 | |
584 | sub hello : Global { |
585 | my ( $self, $c ) = @_; |
586 | $c->stash->{message} = 'Hello World!'; |
d08ced28 |
587 | $c->forward('check_message',[qw/test1/]); |
588 | # now $c->req->args is back to what it was before |
3323f920 |
589 | } |
590 | |
d08ced28 |
591 | sub check_message : Private { |
592 | my ( $self, $c ) = @_; |
593 | my $first_argument = $c->req->args[0]; # now = 'test1' |
594 | # do something... |
595 | } |
cda8d1ac |
596 | |
d08ced28 |
597 | As you can see from these examples, you can just use the method name as |
598 | long as you are referring to methods in the same controller. If you want |
599 | to forward to a method in another controller, or the main application, |
600 | you will have to refer to the method by absolute path. |
cda8d1ac |
601 | |
602 | $c->forward('/my/controller/action'); |
d08ced28 |
603 | $c->forward('/default'); # calls default in main application |
fc7ec1d9 |
604 | |
d08ced28 |
605 | Here are some examples of how to forward to classes and methods. |
fc7ec1d9 |
606 | |
e3dc9d78 |
607 | sub hello : Global { |
5a8ed4fe |
608 | my ( $self, $c ) = @_; |
609 | $c->forward(qw/MyApp::M::Hello say_hello/); |
610 | } |
fc7ec1d9 |
611 | |
e3dc9d78 |
612 | sub bye : Global { |
5a8ed4fe |
613 | my ( $self, $c ) = @_; |
d08ced28 |
614 | $c->forward('MyApp::M::Hello'); # no method: will try 'process' |
5a8ed4fe |
615 | } |
fc7ec1d9 |
616 | |
617 | package MyApp::M::Hello; |
618 | |
619 | sub say_hello { |
620 | my ( $self, $c ) = @_; |
66f6e959 |
621 | $c->res->body('Hello World!'); |
fc7ec1d9 |
622 | } |
623 | |
624 | sub process { |
625 | my ( $self, $c ) = @_; |
66f6e959 |
626 | $c->res->body('Goodbye World!'); |
fc7ec1d9 |
627 | } |
628 | |
d08ced28 |
629 | Note that C<forward> returns to the calling action and continues |
13436c14 |
630 | processing after the action finishes. If you want all further processing |
631 | in the calling action to stop, use C<detach> instead, which will execute |
632 | the C<detach>ed action and not return to the calling sub. In both cases, |
633 | Catalyst will automatically try to call process() if you omit the |
634 | method. |
fc7ec1d9 |
635 | |
636 | =head3 Components |
637 | |
129cfe74 |
638 | Catalyst has an uncommonly flexible component system. You can define as many |
639 | L<Models>, L<Views>, and L<Controllers> as you like. |
fc7ec1d9 |
640 | |
129cfe74 |
641 | All components must inherit from L<Catalyst::Base>, which provides a simple |
642 | class structure and some common class methods like C<config> and C<new> |
643 | (constructor). |
fc7ec1d9 |
644 | |
ac4a0ae0 |
645 | package MyApp::C::Catalog; |
fc7ec1d9 |
646 | |
647 | use strict; |
648 | use base 'Catalyst::Base'; |
649 | |
650 | __PACKAGE__->config( foo => 'bar' ); |
651 | |
652 | 1; |
653 | |
6b10c72b |
654 | You don't have to C<use> or otherwise register Models, Views, and |
655 | Controllers. Catalyst automatically discovers and instantiates them |
656 | when you call C<setup> in the main application. All you need to do is |
657 | put them in directories named for each Component type. Notice that you |
658 | can use some very terse aliases for each one. |
fc7ec1d9 |
659 | |
660 | =over 4 |
661 | |
4a6895ce |
662 | =item * B<MyApp/Model/> |
fc7ec1d9 |
663 | |
4a6895ce |
664 | =item * B<MyApp/M/> |
fc7ec1d9 |
665 | |
4a6895ce |
666 | =item * B<MyApp/View/> |
fc7ec1d9 |
667 | |
4a6895ce |
668 | =item * B<MyApp/V/> |
fc7ec1d9 |
669 | |
4a6895ce |
670 | =item * B<MyApp/Controller/> |
fc7ec1d9 |
671 | |
4a6895ce |
672 | =item * B<MyApp/C/> |
fc7ec1d9 |
673 | |
674 | =back |
675 | |
676 | =head4 Views |
677 | |
129cfe74 |
678 | To show how to define views, we'll use an already-existing base class for the |
679 | L<Template Toolkit|Template>, L<Catalyst::View::TT>. All we need to do is |
680 | inherit from this class: |
fc7ec1d9 |
681 | |
682 | package MyApp::V::TT; |
683 | |
684 | use strict; |
685 | use base 'Catalyst::View::TT'; |
686 | |
687 | 1; |
688 | |
b33ed88c |
689 | (You can also generate this automatically by using the helper script: |
690 | |
691 | script/myapp_create.pl view TT TT |
692 | |
fb9257c1 |
693 | where the first C<TT> tells the script that the name of the view should |
694 | be C<TT>, and the second that it should be a Template Toolkit view.) |
b33ed88c |
695 | |
129cfe74 |
696 | This gives us a process() method and we can now just do |
697 | $c->forward('MyApp::V::TT') to render our templates. The base class makes |
698 | process() implicit, so we don't have to say C<$c-E<gt>forward(qw/MyApp::V::TT |
699 | process/)>. |
fc7ec1d9 |
700 | |
e3dc9d78 |
701 | sub hello : Global { |
5a8ed4fe |
702 | my ( $self, $c ) = @_; |
703 | $c->stash->{template} = 'hello.tt'; |
704 | } |
fc7ec1d9 |
705 | |
5a8ed4fe |
706 | sub end : Private { |
707 | my ( $self, $c ) = @_; |
2feb6632 |
708 | $c->forward('MyApp::V::TT'); |
5a8ed4fe |
709 | } |
fc7ec1d9 |
710 | |
6b10c72b |
711 | You normally render templates at the end of a request, so it's a perfect |
712 | use for the global C<end> action. |
fc7ec1d9 |
713 | |
129cfe74 |
714 | Also, be sure to put the template under the directory specified in |
6b10c72b |
715 | C<$c-E<gt>config-E<gt>{root}>, or you'll be forced to look at our |
716 | eyecandy debug screen. ;) |
fc7ec1d9 |
717 | |
718 | =head4 Models |
719 | |
129cfe74 |
720 | To show how to define models, again we'll use an already-existing base class, |
721 | this time for L<Class::DBI>: L<Catalyst::Model::CDBI>. |
fc7ec1d9 |
722 | |
723 | But first, we need a database. |
724 | |
725 | -- myapp.sql |
726 | CREATE TABLE foo ( |
727 | id INTEGER PRIMARY KEY, |
728 | data TEXT |
729 | ); |
730 | |
731 | CREATE TABLE bar ( |
732 | id INTEGER PRIMARY KEY, |
733 | foo INTEGER REFERENCES foo, |
734 | data TEXT |
735 | ); |
736 | |
737 | INSERT INTO foo (data) VALUES ('TEST!'); |
738 | |
739 | |
740 | % sqlite /tmp/myapp.db < myapp.sql |
741 | |
742 | Now we can create a CDBI component for this database. |
743 | |
744 | package MyApp::M::CDBI; |
745 | |
746 | use strict; |
747 | use base 'Catalyst::Model::CDBI'; |
748 | |
749 | __PACKAGE__->config( |
750 | dsn => 'dbi:SQLite:/tmp/myapp.db', |
751 | relationships => 1 |
752 | ); |
753 | |
754 | 1; |
755 | |
129cfe74 |
756 | Catalyst automatically loads table layouts and relationships. Use the stash to |
757 | pass data to your templates. |
fc7ec1d9 |
758 | |
759 | package MyApp; |
760 | |
761 | use strict; |
762 | use Catalyst '-Debug'; |
763 | |
764 | __PACKAGE__->config( |
765 | name => 'My Application', |
766 | root => '/home/joeuser/myapp/root' |
767 | ); |
cda8d1ac |
768 | |
769 | __PACKAGE__->setup; |
fc7ec1d9 |
770 | |
5a8ed4fe |
771 | sub end : Private { |
772 | my ( $self, $c ) = @_; |
773 | $c->stash->{template} ||= 'index.tt'; |
774 | $c->forward('MyApp::V::TT'); |
775 | } |
fc7ec1d9 |
776 | |
e3dc9d78 |
777 | sub view : Global { |
5a8ed4fe |
778 | my ( $self, $c, $id ) = @_; |
779 | $c->stash->{item} = MyApp::M::CDBI::Foo->retrieve($id); |
780 | } |
fc7ec1d9 |
781 | |
782 | 1; |
783 | |
6b10c72b |
784 | # Then, in a TT template: |
fc7ec1d9 |
785 | The id is [% item.data %] |
786 | |
6b10c72b |
787 | Models do not have to be part of your Catalyst application; you |
788 | can always call an outside module that serves as your Model: |
789 | |
790 | # in a Controller |
791 | sub list : Local { |
792 | my ( $self, $c ) = @_; |
793 | $c->stash->{template} = 'list.tt'; |
794 | use Some::Outside::CDBI::Module; |
795 | my @records = Some::Outside::CDBI::Module->retrieve_all; |
796 | $c->stash->{records} = \@records; |
797 | } |
798 | |
799 | But by using a Model that is part of your Catalyst application, you gain |
800 | several things: you don't have to C<use> each component, Catalyst will |
801 | find and load it automatically at compile-time; you can C<forward> to |
26e73131 |
802 | the module, which can only be done to Catalyst components; and only |
6b10c72b |
803 | Catalyst components can be fetched with |
804 | C<$c-E<gt>comp('MyApp::M::SomeModel')>. |
805 | |
806 | Happily, since many people have existing Model classes that they |
807 | would like to use with Catalyst (or, conversely, they want to |
808 | write Catalyst models that can be used outside of Catalyst, e.g. |
809 | in a cron job), it's trivial to write a simple component in |
810 | Catalyst that slurps in an outside Model: |
811 | |
812 | package MyApp::M::Catalog; |
813 | use base qw/Catalyst::Base Some::Other::CDBI::Module::Catalog/; |
814 | 1; |
815 | |
816 | and that's it! Now C<Some::Other::CDBI::Module::Catalog> is part of your |
817 | Cat app as C<MyApp::M::Catalog>. |
818 | |
fc7ec1d9 |
819 | =head4 Controllers |
820 | |
129cfe74 |
821 | Multiple controllers are a good way to separate logical domains of your |
822 | application. |
fc7ec1d9 |
823 | |
824 | package MyApp::C::Login; |
825 | |
fb9257c1 |
826 | sub sign-in : Local { } |
827 | sub new-password : Local { } |
828 | sub sign-out : Local { } |
fc7ec1d9 |
829 | |
830 | package MyApp::C::Catalog; |
831 | |
e3dc9d78 |
832 | sub view : Local { } |
833 | sub list : Local { } |
fc7ec1d9 |
834 | |
835 | package MyApp::C::Cart; |
836 | |
e3dc9d78 |
837 | sub add : Local { } |
838 | sub update : Local { } |
839 | sub order : Local { } |
fc7ec1d9 |
840 | |
841 | =head3 Testing |
842 | |
129cfe74 |
843 | Catalyst has a built-in http server for testing! (Later, you can easily use a |
844 | more powerful server, e.g. Apache/mod_perl, in a production environment.) |
fc7ec1d9 |
845 | |
846 | Start your application on the command line... |
847 | |
b33ed88c |
848 | script/myapp_server.pl |
fc7ec1d9 |
849 | |
850 | ...then visit http://localhost:3000/ in a browser to view the output. |
851 | |
852 | You can also do it all from the command line: |
853 | |
b33ed88c |
854 | script/myapp_test.pl http://localhost/ |
fc7ec1d9 |
855 | |
856 | Have fun! |
857 | |
3cb1db8c |
858 | =head1 SUPPORT |
859 | |
860 | IRC: |
861 | |
862 | Join #catalyst on irc.perl.org. |
863 | |
72d9bfc7 |
864 | Mailing-lists: |
3cb1db8c |
865 | |
866 | http://lists.rawmode.org/mailman/listinfo/catalyst |
867 | http://lists.rawmode.org/mailman/listinfo/catalyst-dev |
868 | |
fc7ec1d9 |
869 | =head1 AUTHOR |
870 | |
cda8d1ac |
871 | Sebastian Riedel, C<sri@oook.de> |
872 | David Naughton, C<naughton@umn.edu> |
873 | Marcus Ramberg, C<mramberg@cpan.org> |
f531dd37 |
874 | Jesse Sheidlower, C<jester@panix.com> |
129cfe74 |
875 | Danijel Milicevic, C<me@danijel.de> |
fc7ec1d9 |
876 | |
877 | =head1 COPYRIGHT |
878 | |
879 | This program is free software, you can redistribute it and/or modify it under |
880 | the same terms as Perl itself. |