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 | |
129cfe74 |
46 | If you're unfamiliar with MVC and design patterns, you may want to check out the |
47 | original book on the subject, I<Design Patterns>, by Gamma, Helm, Johson and |
48 | Vlissides, also known as the Gang of Four (GoF). You can also just google it. |
49 | Many, many web application frameworks are based on MVC, including all those |
50 | 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 ) = @_; |
87 | $context->response->output('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', |
187 | root => '/home/joeuser/myapp/root', |
188 | |
b33ed88c |
189 | # You can put anything else you want in here: |
190 | my_configuration_variable => 'something', |
fc7ec1d9 |
191 | ); |
192 | |
5a8ed4fe |
193 | sub default : Private { |
fc7ec1d9 |
194 | my ( $self, $context ) = @_; |
195 | $context->response->output('Catalyst rockz!'); |
5a8ed4fe |
196 | } |
fc7ec1d9 |
197 | |
198 | 1; |
199 | |
129cfe74 |
200 | For most applications, Catalyst requires you to define only two config |
201 | parameters: |
fc7ec1d9 |
202 | |
203 | =over 4 |
204 | |
4a6895ce |
205 | =item * B<name> |
fc7ec1d9 |
206 | |
207 | Name of your application. |
208 | |
4a6895ce |
209 | =item * B<root> |
fc7ec1d9 |
210 | |
72d9bfc7 |
211 | Path to additional files such as templates, images, or other static data. |
fc7ec1d9 |
212 | |
213 | =back |
214 | |
129cfe74 |
215 | However, you can define as many parameters as you want for plugins or whatever |
216 | you need. You can access them anywhere in your application via |
217 | C<$context-E<gt>config-E<gt>{$param_name}>. |
fc7ec1d9 |
218 | |
219 | =head3 Context |
220 | |
129cfe74 |
221 | Catalyst automatically blesses a Context object into your application class and |
222 | makes it available everywhere in your application. Use the Context to directly |
223 | interact with Catalyst and glue your L<Components> together. For example, if you |
224 | need to use the Context from within a Template Toolkit template, it's already |
225 | there: |
c42f5bbf |
226 | |
227 | <h1>Welcome to [% c.config.name %]!</h1> |
fc7ec1d9 |
228 | |
129cfe74 |
229 | As illustrated earlier in our URL-to-Action dispatching example, the Context is |
230 | always the second method parameter, behind the Component object reference or |
231 | class name itself. Previously we called it C<$context> for clarity, but most |
232 | Catalyst developers just call it C<$c>: |
fc7ec1d9 |
233 | |
e3dc9d78 |
234 | sub hello : Global { |
fc7ec1d9 |
235 | my ( $self, $c ) = @_; |
236 | $c->res->output('Hello World!'); |
5a8ed4fe |
237 | } |
fc7ec1d9 |
238 | |
239 | The Context contains several important objects: |
240 | |
241 | =over 4 |
242 | |
243 | =item * L<Catalyst::Request> |
244 | |
245 | $c->request |
246 | $c->req # alias |
247 | |
129cfe74 |
248 | The request object contains all kinds of request-specific information, like |
249 | query parameters, cookies, uploads, headers, and more. |
fc7ec1d9 |
250 | |
251 | $c->req->params->{foo}; |
252 | $c->req->cookies->{sessionid}; |
253 | $c->req->headers->content_type; |
254 | $c->req->base; |
255 | |
afdca3a3 |
256 | =item * L<Catalyst::Response> |
fc7ec1d9 |
257 | |
258 | $c->response |
259 | $c->res # alias |
260 | |
129cfe74 |
261 | The response is like the request, but contains just response-specific |
262 | information. |
fc7ec1d9 |
263 | |
264 | $c->res->output('Hello World'); |
265 | $c->res->status(404); |
266 | $c->res->redirect('http://oook.de'); |
267 | |
268 | =item * L<Catalyst::Config> |
269 | |
270 | $c->config |
271 | |
272 | $c->config->root; |
273 | $c->config->name; |
274 | |
275 | =item * L<Catalyst::Log> |
276 | |
277 | $c->log |
278 | |
279 | $c->log->debug('Something happened'); |
280 | $c->log->info('Something you should know'); |
281 | |
4a6895ce |
282 | =item * B<Stash> |
fc7ec1d9 |
283 | |
284 | $c->stash |
285 | |
286 | $c->stash->{foo} = 'bar'; |
287 | |
288 | =back |
289 | |
129cfe74 |
290 | The last of these, the stash, is a universal hash for sharing data among |
291 | application components. For an example, we return to our 'hello' action: |
fc7ec1d9 |
292 | |
e3dc9d78 |
293 | sub hello : Global { |
5a8ed4fe |
294 | my ( $self, $c ) = @_; |
295 | $c->stash->{message} = 'Hello World!'; |
4c6807d2 |
296 | $c->forward('show_message'); |
5a8ed4fe |
297 | } |
fc7ec1d9 |
298 | |
4c6807d2 |
299 | sub show_message : Private { |
5a8ed4fe |
300 | my ( $self, $c ) = @_; |
301 | $c->res->output( $c->stash->{message} ); |
302 | } |
fc7ec1d9 |
303 | |
129cfe74 |
304 | Note that the stash should be used only for passing data in an individual |
305 | request cycle; it gets cleared at a new request. If you need to maintain more |
306 | persistent data, use a session. |
dd25a192 |
307 | |
fc7ec1d9 |
308 | =head3 Actions |
309 | |
129cfe74 |
310 | A Catalyst controller is defined by its actions. An action is a sub with a |
311 | special attribute. You've already seen some examples of actions in this |
312 | document. The URL (for example http://localhost.3000/foo/bar) consists of two |
313 | parts, the base (http://localhost:3000/ in this example) and the path (foo/bar). |
314 | Please note that the trailing slash after the hostname[:port] always belongs to |
315 | base and not to the action. |
cda8d1ac |
316 | |
317 | Catalyst supports several types of actions: |
fc7ec1d9 |
318 | |
319 | =over 4 |
320 | |
4a6895ce |
321 | =item * B<Literal> |
fc7ec1d9 |
322 | |
f29c48dd |
323 | sub bar : Path('foo/bar') { } |
fc7ec1d9 |
324 | |
325 | Matches only http://localhost:3000/foo/bar. |
326 | |
4a6895ce |
327 | =item * B<Regex> |
fc7ec1d9 |
328 | |
b33ed88c |
329 | sub bar : Regex('^item(\d+)/order(\d+)$') { } |
fc7ec1d9 |
330 | |
129cfe74 |
331 | Matches any URL that matches the pattern in the action key, e.g. |
332 | http://localhost:3000/item23/order42. The '' around the regexp is optional, but |
333 | perltidy likes it. :) |
b33ed88c |
334 | |
129cfe74 |
335 | Regex matches act globally, i.e. without reference to the namespace from which |
336 | it is called, so that a C<bar> method in the |
337 | C<MyApp::Controller::Catalog::Order::Process> namespace won't match any form of |
338 | C<bar>, C<Catalog>, C<Order>, or C<Process> unless you explicitly put this in |
339 | the regex. |
fc7ec1d9 |
340 | |
129cfe74 |
341 | If you use capturing parentheses to extract values within the matching URL (23, |
342 | 42 in the above example), those values are available in the $c->req->snippets |
343 | array. If you want to pass arguments at the end of your URL, you must use regex |
344 | action keys. See L</URL Argument Handling> below. |
fc7ec1d9 |
345 | |
72d9bfc7 |
346 | =item * B<Top-level> |
cda8d1ac |
347 | |
348 | package MyApp; |
349 | sub foo : Global { } |
350 | |
b33ed88c |
351 | Matches http://localhost:3000/foo. The function name is mapped directly |
352 | to the application base. |
cda8d1ac |
353 | |
4a6895ce |
354 | =item * B<Namespace-Prefixed> |
fc7ec1d9 |
355 | |
2feb6632 |
356 | package MyApp::C::My::Controller; |
e3dc9d78 |
357 | sub foo : Local { } |
fc7ec1d9 |
358 | |
cda8d1ac |
359 | Matches http://localhost:3000/my/controller/foo. |
fc7ec1d9 |
360 | |
129cfe74 |
361 | This action type indicates that the matching URL must be prefixed with a |
362 | modified form of the component's class (package) name. This modified class name |
363 | excludes the parts that have a pre-defined meaning in Catalyst ("MyApp::C" in |
364 | the above example), replaces "::" with "/", and converts the name to lower case. |
365 | See L</Components> for a full explanation of the pre-defined meaning of Catalyst |
366 | component class names. |
fc7ec1d9 |
367 | |
4a6895ce |
368 | =item * B<Private> |
fc7ec1d9 |
369 | |
5a8ed4fe |
370 | sub foo : Private { } |
fc7ec1d9 |
371 | |
129cfe74 |
372 | Matches no URL, and cannot be executed by requesting a URL that corresponds to |
373 | the action key. Private actions can be executed only inside a Catalyst |
374 | application, by calling the C<forward> method: |
fc7ec1d9 |
375 | |
5a8ed4fe |
376 | $c->forward('foo'); |
fc7ec1d9 |
377 | |
129cfe74 |
378 | See L</Flow Control> for a full explanation of C<forward>. Note that, as |
379 | discussed there, when forwarding from another component, you must use the |
380 | absolute path to the method, so that a private C<bar> method in your |
381 | C<MyApp::Controller::Catalog::Order::Process> controller must, if called from |
382 | elsewhere, be reach with C<$c-E<gt>forward('/catalog/order/process/bar')>. |
fc7ec1d9 |
383 | |
384 | =back |
385 | |
b33ed88c |
386 | B<Note:> After seeing these examples, you probably wonder what the point |
387 | is of defining names for regex and path actions. Actually, every public |
388 | action is also a private one, so you have one unified way of addressing |
389 | components in your C<forward>s. |
cda8d1ac |
390 | |
72d9bfc7 |
391 | =head4 Built-in Private Actions |
fc7ec1d9 |
392 | |
129cfe74 |
393 | In response to specific application states, Catalyst will automatically call |
394 | these built-in private actions: |
fc7ec1d9 |
395 | |
396 | =over 4 |
397 | |
cda8d1ac |
398 | =item * B<default : Private> |
fc7ec1d9 |
399 | |
400 | Called when no other action matches. |
401 | |
cda8d1ac |
402 | =item * B<begin : Private> |
fc7ec1d9 |
403 | |
404 | Called at the beginning of a request, before any matching actions are called. |
405 | |
cda8d1ac |
406 | =item * B<end : Private> |
4a6895ce |
407 | |
408 | =back |
fc7ec1d9 |
409 | |
410 | Called at the end of a request, after all matching actions are called. |
411 | |
72d9bfc7 |
412 | =head4 B<Built-in actions in controllers/autochaining> |
fc7ec1d9 |
413 | |
cda8d1ac |
414 | Package MyApp::C::Foo; |
415 | sub begin : Private { } |
5a8ed4fe |
416 | sub default : Private { } |
fc7ec1d9 |
417 | |
72d9bfc7 |
418 | You can define the Built-in Private Actions within your controllers as |
b33ed88c |
419 | well. The actions will override the ones in lower level controllers or |
420 | your global application. |
80ef2e6d |
421 | |
72d9bfc7 |
422 | In addition to the normal built-ins, you have a special action for |
423 | making inheritance chains, 'auto'. These will be run after C<begin>, |
80ef2e6d |
424 | but before your action is processed. |
cda8d1ac |
425 | |
426 | =over 4 |
427 | |
428 | =item for a request for /foo/foo |
429 | |
430 | MyApp::begin |
80ef2e6d |
431 | MyApp::auto |
42a57832 |
432 | MyApp::C::Foo::default |
cda8d1ac |
433 | MyApp::end |
434 | |
435 | =item for a request for /foo/bar/foo |
436 | |
cda8d1ac |
437 | MyApp::C::Foo::Bar::begin |
80ef2e6d |
438 | MyApp::auto |
439 | MyApp::C::Foo::auto |
cda8d1ac |
440 | MyApp::C::Foo::Bar::default |
441 | MyApp::C::Foo::Bar::end |
80ef2e6d |
442 | |
443 | =back |
444 | |
445 | Also, if you need to break out of the chain in one of your auto |
446 | actions, you can return 0, if so, your action will not be processed, |
447 | but the end will, so for the request above, if the first auto returns |
448 | false, it would look like this: |
449 | |
450 | =over 4 |
451 | |
452 | =item for a request for /foo/bar/foo where auto returns false |
453 | |
454 | MyApp::C::Foo::Bar::begin |
455 | MyApp::auto |
456 | MyApp::C::Foo::Bar::end |
cda8d1ac |
457 | |
458 | =back |
4a6895ce |
459 | |
b33ed88c |
460 | B<Note:> auto actions have to return a true value to continue |
461 | processing! You can also die in the autochain action, in that case, the |
462 | request will go straight to the finalize stage, without processing |
03805733 |
463 | further actions. |
464 | |
465 | |
4a6895ce |
466 | =head4 B<URL Argument Handling> |
467 | |
129cfe74 |
468 | If you want to pass variable arguments at the end of a URL, you must use regex |
469 | actions keys with '^' and '$' anchors, and the arguments must be separated with |
470 | forward slashes (/) in the URL. For example, suppose you want to handle |
471 | /foo/$bar/$baz, where $bar and $baz may vary: |
4a6895ce |
472 | |
cda8d1ac |
473 | sub foo : Regex('^foo$') { my ($self, $context, $bar, $baz) = @_; } |
4a6895ce |
474 | |
475 | But what if you also defined actions for /foo/boo and /foo/boo/hoo ? |
476 | |
f29c48dd |
477 | sub boo : Path('foo/boo') { .. } |
478 | sub hoo : Path('foo/boo/hoo') { .. } |
4a6895ce |
479 | |
480 | Catalyst matches actions in most specific to least specific order: |
481 | |
482 | /foo/boo/hoo |
483 | /foo/boo |
484 | /foo # might be /foo/bar/baz |
485 | |
cda8d1ac |
486 | So Catalyst would never mistakenly dispatch the first two URLs to the '^foo$' action. |
fc7ec1d9 |
487 | |
2ef2fb0f |
488 | =head4 B<Parameter Processing> |
489 | |
490 | Parameters are handled with methods in the L<Catalyst::Request> |
491 | class. The C<param> method is functionally equivalent to the C<param> |
492 | method of C<CGI.pm> and can be used in modules that require this. |
493 | |
494 | # http://localhost:3000/catalog/view/?category=hardware&page=3 |
495 | my $category = $c->req->param('category'); |
496 | my $current_page = $c->req->param('page') || 1; |
497 | |
498 | # multiple values for single parameter name |
499 | my @values = $c->req->param('scrolling_list'); |
500 | |
501 | # DFV requires a CGI.pm-like input hash |
502 | my $results = Data::FormValidator->check($c->req->params, \%dfv_profile); |
503 | |
fc7ec1d9 |
504 | =head3 Flow Control |
505 | |
129cfe74 |
506 | You control the application flow with the C<forward> method, which accepts the |
507 | key of an action to execute. |
fc7ec1d9 |
508 | |
e3dc9d78 |
509 | sub hello : Global { |
5a8ed4fe |
510 | my ( $self, $c ) = @_; |
511 | $c->stash->{message} = 'Hello World!'; |
4c6807d2 |
512 | $c->forward('check_message'); |
5a8ed4fe |
513 | } |
fc7ec1d9 |
514 | |
4c6807d2 |
515 | sub check_message : Private { |
5a8ed4fe |
516 | my ( $self, $c ) = @_; |
517 | return unless $c->stash->{message}; |
4c6807d2 |
518 | $c->forward('show_message'); |
5a8ed4fe |
519 | } |
fc7ec1d9 |
520 | |
4c6807d2 |
521 | sub show_message : Private { |
5a8ed4fe |
522 | my ( $self, $c ) = @_; |
523 | $c->res->output( $c->stash->{message} ); |
524 | } |
cda8d1ac |
525 | |
129cfe74 |
526 | As you can see from these examples, you can just use the method name as long as |
527 | you are referring to methods in the same controller. If you want to forward to a |
528 | method in another controller, or the main application, you will have to refer to |
529 | the method by absolute path. |
cda8d1ac |
530 | |
531 | $c->forward('/my/controller/action'); |
532 | $c->forward('/default'); |
fc7ec1d9 |
533 | |
534 | You can also forward to classes and methods. |
535 | |
e3dc9d78 |
536 | sub hello : Global { |
5a8ed4fe |
537 | my ( $self, $c ) = @_; |
538 | $c->forward(qw/MyApp::M::Hello say_hello/); |
539 | } |
fc7ec1d9 |
540 | |
e3dc9d78 |
541 | sub bye : Global { |
5a8ed4fe |
542 | my ( $self, $c ) = @_; |
543 | $c->forward('MyApp::M::Hello'); |
544 | } |
fc7ec1d9 |
545 | |
546 | package MyApp::M::Hello; |
547 | |
548 | sub say_hello { |
549 | my ( $self, $c ) = @_; |
550 | $c->res->output('Hello World!'); |
551 | } |
552 | |
553 | sub process { |
554 | my ( $self, $c ) = @_; |
555 | $c->res->output('Goodbye World!'); |
556 | } |
557 | |
129cfe74 |
558 | Note that C<forward> returns to the calling action and continues processing |
559 | after the action finishes. Catalyst will automatically try to call process() if |
560 | you omit the method. |
fc7ec1d9 |
561 | |
562 | =head3 Components |
563 | |
129cfe74 |
564 | Catalyst has an uncommonly flexible component system. You can define as many |
565 | L<Models>, L<Views>, and L<Controllers> as you like. |
fc7ec1d9 |
566 | |
129cfe74 |
567 | All components must inherit from L<Catalyst::Base>, which provides a simple |
568 | class structure and some common class methods like C<config> and C<new> |
569 | (constructor). |
fc7ec1d9 |
570 | |
ac4a0ae0 |
571 | package MyApp::C::Catalog; |
fc7ec1d9 |
572 | |
573 | use strict; |
574 | use base 'Catalyst::Base'; |
575 | |
576 | __PACKAGE__->config( foo => 'bar' ); |
577 | |
578 | 1; |
579 | |
129cfe74 |
580 | You don't have to C<use> or otherwise register Models, Views, and Controllers. |
581 | Catalyst automatically discovers and instantiates them when you call C<setup> in |
582 | the main application. All you need to do is put them in directories named for |
583 | each Component type. Notice that you can use some very terse aliases for each |
584 | one. |
fc7ec1d9 |
585 | |
586 | =over 4 |
587 | |
4a6895ce |
588 | =item * B<MyApp/Model/> |
fc7ec1d9 |
589 | |
4a6895ce |
590 | =item * B<MyApp/M/> |
fc7ec1d9 |
591 | |
4a6895ce |
592 | =item * B<MyApp/View/> |
fc7ec1d9 |
593 | |
4a6895ce |
594 | =item * B<MyApp/V/> |
fc7ec1d9 |
595 | |
4a6895ce |
596 | =item * B<MyApp/Controller/> |
fc7ec1d9 |
597 | |
4a6895ce |
598 | =item * B<MyApp/C/> |
fc7ec1d9 |
599 | |
600 | =back |
601 | |
602 | =head4 Views |
603 | |
129cfe74 |
604 | To show how to define views, we'll use an already-existing base class for the |
605 | L<Template Toolkit|Template>, L<Catalyst::View::TT>. All we need to do is |
606 | inherit from this class: |
fc7ec1d9 |
607 | |
608 | package MyApp::V::TT; |
609 | |
610 | use strict; |
611 | use base 'Catalyst::View::TT'; |
612 | |
613 | 1; |
614 | |
b33ed88c |
615 | (You can also generate this automatically by using the helper script: |
616 | |
617 | script/myapp_create.pl view TT TT |
618 | |
619 | where the first C<TT> tells the script to create a Template Toolkit |
620 | view, and the second tells the script that its name should be C<TT>.) |
621 | |
129cfe74 |
622 | This gives us a process() method and we can now just do |
623 | $c->forward('MyApp::V::TT') to render our templates. The base class makes |
624 | process() implicit, so we don't have to say C<$c-E<gt>forward(qw/MyApp::V::TT |
625 | process/)>. |
fc7ec1d9 |
626 | |
e3dc9d78 |
627 | sub hello : Global { |
5a8ed4fe |
628 | my ( $self, $c ) = @_; |
629 | $c->stash->{template} = 'hello.tt'; |
630 | } |
fc7ec1d9 |
631 | |
5a8ed4fe |
632 | sub end : Private { |
633 | my ( $self, $c ) = @_; |
2feb6632 |
634 | $c->forward('MyApp::V::TT'); |
5a8ed4fe |
635 | } |
fc7ec1d9 |
636 | |
129cfe74 |
637 | You normally render templates at the end of a request, so it's a perfect use for |
638 | the global C<end> action. |
fc7ec1d9 |
639 | |
129cfe74 |
640 | Also, be sure to put the template under the directory specified in |
641 | C<$c-E<gt>config-E<gt>{root}>, or you'll be forced to look at our eyecandy debug |
642 | screen. ;) |
fc7ec1d9 |
643 | |
644 | =head4 Models |
645 | |
129cfe74 |
646 | To show how to define models, again we'll use an already-existing base class, |
647 | this time for L<Class::DBI>: L<Catalyst::Model::CDBI>. |
fc7ec1d9 |
648 | |
649 | But first, we need a database. |
650 | |
651 | -- myapp.sql |
652 | CREATE TABLE foo ( |
653 | id INTEGER PRIMARY KEY, |
654 | data TEXT |
655 | ); |
656 | |
657 | CREATE TABLE bar ( |
658 | id INTEGER PRIMARY KEY, |
659 | foo INTEGER REFERENCES foo, |
660 | data TEXT |
661 | ); |
662 | |
663 | INSERT INTO foo (data) VALUES ('TEST!'); |
664 | |
665 | |
666 | % sqlite /tmp/myapp.db < myapp.sql |
667 | |
668 | Now we can create a CDBI component for this database. |
669 | |
670 | package MyApp::M::CDBI; |
671 | |
672 | use strict; |
673 | use base 'Catalyst::Model::CDBI'; |
674 | |
675 | __PACKAGE__->config( |
676 | dsn => 'dbi:SQLite:/tmp/myapp.db', |
677 | relationships => 1 |
678 | ); |
679 | |
680 | 1; |
681 | |
129cfe74 |
682 | Catalyst automatically loads table layouts and relationships. Use the stash to |
683 | pass data to your templates. |
fc7ec1d9 |
684 | |
685 | package MyApp; |
686 | |
687 | use strict; |
688 | use Catalyst '-Debug'; |
689 | |
690 | __PACKAGE__->config( |
691 | name => 'My Application', |
692 | root => '/home/joeuser/myapp/root' |
693 | ); |
cda8d1ac |
694 | |
695 | __PACKAGE__->setup; |
fc7ec1d9 |
696 | |
5a8ed4fe |
697 | sub end : Private { |
698 | my ( $self, $c ) = @_; |
699 | $c->stash->{template} ||= 'index.tt'; |
700 | $c->forward('MyApp::V::TT'); |
701 | } |
fc7ec1d9 |
702 | |
e3dc9d78 |
703 | sub view : Global { |
5a8ed4fe |
704 | my ( $self, $c, $id ) = @_; |
705 | $c->stash->{item} = MyApp::M::CDBI::Foo->retrieve($id); |
706 | } |
fc7ec1d9 |
707 | |
708 | 1; |
709 | |
710 | The id is [% item.data %] |
711 | |
712 | =head4 Controllers |
713 | |
129cfe74 |
714 | Multiple controllers are a good way to separate logical domains of your |
715 | application. |
fc7ec1d9 |
716 | |
717 | package MyApp::C::Login; |
718 | |
72d9bfc7 |
719 | sign-in : Local { } |
720 | new-password : Local { } |
721 | sign-out : Local { } |
fc7ec1d9 |
722 | |
723 | package MyApp::C::Catalog; |
724 | |
e3dc9d78 |
725 | sub view : Local { } |
726 | sub list : Local { } |
fc7ec1d9 |
727 | |
728 | package MyApp::C::Cart; |
729 | |
e3dc9d78 |
730 | sub add : Local { } |
731 | sub update : Local { } |
732 | sub order : Local { } |
fc7ec1d9 |
733 | |
734 | =head3 Testing |
735 | |
129cfe74 |
736 | Catalyst has a built-in http server for testing! (Later, you can easily use a |
737 | more powerful server, e.g. Apache/mod_perl, in a production environment.) |
fc7ec1d9 |
738 | |
739 | Start your application on the command line... |
740 | |
b33ed88c |
741 | script/myapp_server.pl |
fc7ec1d9 |
742 | |
743 | ...then visit http://localhost:3000/ in a browser to view the output. |
744 | |
745 | You can also do it all from the command line: |
746 | |
b33ed88c |
747 | script/myapp_test.pl http://localhost/ |
fc7ec1d9 |
748 | |
749 | Have fun! |
750 | |
3cb1db8c |
751 | =head1 SUPPORT |
752 | |
753 | IRC: |
754 | |
755 | Join #catalyst on irc.perl.org. |
756 | |
72d9bfc7 |
757 | Mailing-lists: |
3cb1db8c |
758 | |
759 | http://lists.rawmode.org/mailman/listinfo/catalyst |
760 | http://lists.rawmode.org/mailman/listinfo/catalyst-dev |
761 | |
fc7ec1d9 |
762 | =head1 AUTHOR |
763 | |
cda8d1ac |
764 | Sebastian Riedel, C<sri@oook.de> |
765 | David Naughton, C<naughton@umn.edu> |
766 | Marcus Ramberg, C<mramberg@cpan.org> |
f531dd37 |
767 | Jesse Sheidlower, C<jester@panix.com> |
129cfe74 |
768 | Danijel Milicevic, C<me@danijel.de> |
fc7ec1d9 |
769 | |
770 | =head1 COPYRIGHT |
771 | |
772 | This program is free software, you can redistribute it and/or modify it under |
773 | the same terms as Perl itself. |