Commit | Line | Data |
fc7ec1d9 |
1 | =head1 NAME |
2 | |
3 | Catalyst::Manual::Intro - Introduction to Catalyst |
4 | |
5 | =head1 DESCRIPTION |
6 | |
72d9bfc7 |
7 | This is a brief overview of why and how to use Catalyst. It explains how Catalyst works and shows how to get a simple application up and running quickly. |
fc7ec1d9 |
8 | |
9 | =head2 What is Catalyst? |
10 | |
11 | Catalyst is an elegant web application framework, extremely flexible yet extremely simple. It's similar to Ruby on Rails, Spring (Java) and L<Maypole>, upon which it was originally based. |
12 | |
13 | =head3 MVC |
14 | |
72d9bfc7 |
15 | Catalyst follows the Model-View-Controller (MVC) design pattern, allowing you to easily separate concerns, like content, presentation, and flow control, into separate modules. This separation allows you to modify code that handles one concern without affecting code that handles the others. Catalyst promotes the re-use of existing Perl modules that already handle common web application concerns well. |
fc7ec1d9 |
16 | |
72d9bfc7 |
17 | Here's how the M, V, and C map to those concerns, with examples of well-known Perl modules you may want to use for each. |
fc7ec1d9 |
18 | |
19 | =over 4 |
20 | |
4a6895ce |
21 | =item * B<Model> |
fc7ec1d9 |
22 | |
23 | Access and modify content (data). L<Class::DBI>, L<Plucene>, L<Net::LDAP>... |
24 | |
4a6895ce |
25 | =item * B<View> |
fc7ec1d9 |
26 | |
c42f5bbf |
27 | Present content to the user. L<Template Toolkit|Template>, L<Mason|HTML::Mason>, L<HTML::Template>... |
fc7ec1d9 |
28 | |
4a6895ce |
29 | =item * B<Controller> |
fc7ec1d9 |
30 | |
31 | Control the whole request phase, check parameters, dispatch actions, flow control. Catalyst! |
32 | |
33 | =back |
34 | |
cda8d1ac |
35 | If you're unfamiliar with MVC and design patterns, you may want to check out the original book on the subject, I<Design Patterns>, by Gamma, Helm, Johson and Vlissides, also known as the Gang of Four (GoF). You can also just google it. Many, many web application frameworks are based on MVC, including all those listed above. |
fc7ec1d9 |
36 | |
37 | =head3 Flexibility |
38 | |
72d9bfc7 |
39 | Catalyst is much more flexible than many other frameworks. We'll talk more about this later, but rest assured you can use your favorite Perl modules with Catalyst. |
fc7ec1d9 |
40 | |
41 | =over 4 |
42 | |
72d9bfc7 |
43 | =item * B<Multiple Models, Views, and Controllers> |
fc7ec1d9 |
44 | |
72d9bfc7 |
45 | To build a Catalyst application, you handle each type of concern inside special modules called L</Components>. Often this code will be very simple, just calling out to Perl modules like those listed above under L</MVC>. Catalyst handles these components in a very flexible way. Use as many Models, Views, and Controllers as you like, using as many different Perl modules as you like, all in the same application. Want to manipulate multiple databases, and retrieve some data via LDAP? No problem. Want to present data from the same Model using L<Template Toolkit|Template> and L<PDF::Template>? Easy. |
fc7ec1d9 |
46 | |
cda8d1ac |
47 | =item * B<Reuseable Components> |
fc7ec1d9 |
48 | |
cda8d1ac |
49 | Not only does Catalyst promote the re-use of already existing Perl modules, it also allows you to re-use your Catalyst components in multiple Catalyst applications. |
fc7ec1d9 |
50 | |
4a6895ce |
51 | =item * B<Unrestrained URL-to-Action Dispatching> |
fc7ec1d9 |
52 | |
cda8d1ac |
53 | Catalyst allows you to dispatch any URLs to any application L<Actions>, even through regular expressions! Unlike most other frameworks, it doesn't require mod_rewrite or class and method names in URLs. |
fc7ec1d9 |
54 | |
55 | With Catalyst you register your actions and address them directly. For example: |
56 | |
e3dc9d78 |
57 | sub hello : Global { |
fc7ec1d9 |
58 | my ( $self, $context ) = @_; |
59 | $context->response->output('Hello World!'); |
5a8ed4fe |
60 | } |
fc7ec1d9 |
61 | |
62 | Now http://localhost:3000/hello prints "Hello World!". |
63 | |
4a6895ce |
64 | =item * B<Support for CGI, mod_perl, Apache::Request> |
fc7ec1d9 |
65 | |
66 | Use L<Catalyst::Engine::Apache> or L<Catalyst::Engine::CGI>. |
67 | |
68 | =back |
69 | |
70 | =head3 Simplicity |
71 | |
72 | The best part is that Catalyst implements all this flexibility in a very simple way. |
73 | |
6f4e1683 |
74 | =over 4 |
75 | |
4a6895ce |
76 | =item * B<Building Block Interface> |
fc7ec1d9 |
77 | |
cda8d1ac |
78 | Components interoperate very smoothly. For example, Catalyst automatically makes a L<Context> object available to every component. Via the context, you can access the request object, share data between components, and control the flow of your application. Building a Catalyst application feels a lot like snapping together toy building blocks, and everything just works. |
fc7ec1d9 |
79 | |
4a6895ce |
80 | =item * B<Component Auto-Discovery> |
fc7ec1d9 |
81 | |
82 | No need to C<use> all of your components. Catalyst automatically finds and loads them. |
83 | |
4a6895ce |
84 | =item * B<Pre-Built Components for Popular Modules> |
fc7ec1d9 |
85 | |
86 | See L<Catalyst::Model::CDBI> for L<Class::DBI>, or L<Catalyst::View::TT> for L<Template Toolkit|Template>. You can even get an instant web database front end with L<Catalyst::Model::CDBI::CRUD>. |
87 | |
72d9bfc7 |
88 | =item * B<Built-in Test Framework> |
fc7ec1d9 |
89 | |
72d9bfc7 |
90 | Catalyst comes with a built-in, lightweight http server and test framework, making it easy to test applications from the command line. |
fc7ec1d9 |
91 | |
4a6895ce |
92 | =item * B<Helper Scripts> |
fc7ec1d9 |
93 | |
94 | Catalyst provides helper scripts to quickly generate running starter code for components and unit tests. |
95 | |
6f4e1683 |
96 | =back |
97 | |
fc7ec1d9 |
98 | =head2 Quickstart |
99 | |
100 | Here's how to install Catalyst and get a simple application up and running, using the helper scripts described above. |
101 | |
102 | =head3 Install |
103 | |
104 | $ perl -MCPAN -e 'install Bundle::Catalyst' |
105 | |
106 | =head3 Setup |
107 | |
2feb6632 |
108 | $ catalyst.pl MyApp |
b33ed88c |
109 | # output omitted |
2feb6632 |
110 | $ cd MyApp |
ac4a0ae0 |
111 | $ script/myapp_create.pl controller Library::Login |
fc7ec1d9 |
112 | |
113 | =head3 Run |
114 | |
b33ed88c |
115 | $ script/myapp_server.pl |
fc7ec1d9 |
116 | |
117 | Now visit these locations with your favorite browser or user agent to see Catalyst in action: |
118 | |
119 | =over 4 |
120 | |
121 | =item http://localhost:3000/ |
122 | |
ac4a0ae0 |
123 | =item http://localhost:3000/library/login/ |
fc7ec1d9 |
124 | |
125 | =back |
126 | |
127 | Dead easy! |
128 | |
129 | =head2 How It Works |
130 | |
131 | Let's see how Catalyst works, by taking a closer look at the components and other parts of a Catalyst application. |
132 | |
133 | =head3 Application Class |
134 | |
c42f5bbf |
135 | In addition to the Model, View, and Controller components, there's a single class that represents your application itself. This is where you configure your application, load plugins, define application-wide actions, and extend Catalyst. |
fc7ec1d9 |
136 | |
137 | package MyApp; |
138 | |
139 | use strict; |
140 | use Catalyst qw/-Debug/; |
141 | |
142 | MyApp->config( |
143 | name => 'My Application', |
144 | root => '/home/joeuser/myapp/root', |
145 | |
b33ed88c |
146 | # You can put anything else you want in here: |
147 | my_configuration_variable => 'something', |
fc7ec1d9 |
148 | ); |
149 | |
5a8ed4fe |
150 | sub default : Private { |
fc7ec1d9 |
151 | my ( $self, $context ) = @_; |
152 | $context->response->output('Catalyst rockz!'); |
5a8ed4fe |
153 | } |
fc7ec1d9 |
154 | |
155 | 1; |
156 | |
157 | For most applications, Catalyst requires you to define only two config parameters: |
158 | |
159 | =over 4 |
160 | |
4a6895ce |
161 | =item * B<name> |
fc7ec1d9 |
162 | |
163 | Name of your application. |
164 | |
4a6895ce |
165 | =item * B<root> |
fc7ec1d9 |
166 | |
72d9bfc7 |
167 | Path to additional files such as templates, images, or other static data. |
fc7ec1d9 |
168 | |
169 | =back |
170 | |
4a6895ce |
171 | However, you can define as many parameters as you want for plugins or whatever you need. You can access them anywhere in your application via C<$context-E<gt>config-E<gt>{$param_name}>. |
fc7ec1d9 |
172 | |
173 | =head3 Context |
174 | |
c42f5bbf |
175 | Catalyst automatically blesses a Context object into your application class and makes it available everywhere in your application. Use the Context to directly interact with Catalyst and glue your L<Components> together. For example, if you need to use the Context from within a Template Toolkit template, it's already there: |
176 | |
177 | <h1>Welcome to [% c.config.name %]!</h1> |
fc7ec1d9 |
178 | |
4a6895ce |
179 | As illustrated earlier in our URL-to-Action dispatching example, the Context is always the second method parameter, behind the Component object reference or class name itself. Previously we called it C<$context> for clarity, but most Catalyst developers just call it C<$c>: |
fc7ec1d9 |
180 | |
e3dc9d78 |
181 | sub hello : Global { |
fc7ec1d9 |
182 | my ( $self, $c ) = @_; |
183 | $c->res->output('Hello World!'); |
5a8ed4fe |
184 | } |
fc7ec1d9 |
185 | |
186 | The Context contains several important objects: |
187 | |
188 | =over 4 |
189 | |
190 | =item * L<Catalyst::Request> |
191 | |
192 | $c->request |
193 | $c->req # alias |
194 | |
72d9bfc7 |
195 | The request object contains all kinds of request-specific information, like query parameters, cookies, uploads, headers, and more. |
fc7ec1d9 |
196 | |
197 | $c->req->params->{foo}; |
198 | $c->req->cookies->{sessionid}; |
199 | $c->req->headers->content_type; |
200 | $c->req->base; |
201 | |
afdca3a3 |
202 | =item * L<Catalyst::Response> |
fc7ec1d9 |
203 | |
204 | $c->response |
205 | $c->res # alias |
206 | |
4a6895ce |
207 | The response is like the request, but contains just response-specific information. |
fc7ec1d9 |
208 | |
209 | $c->res->output('Hello World'); |
210 | $c->res->status(404); |
211 | $c->res->redirect('http://oook.de'); |
212 | |
213 | =item * L<Catalyst::Config> |
214 | |
215 | $c->config |
216 | |
217 | $c->config->root; |
218 | $c->config->name; |
219 | |
220 | =item * L<Catalyst::Log> |
221 | |
222 | $c->log |
223 | |
224 | $c->log->debug('Something happened'); |
225 | $c->log->info('Something you should know'); |
226 | |
4a6895ce |
227 | =item * B<Stash> |
fc7ec1d9 |
228 | |
229 | $c->stash |
230 | |
231 | $c->stash->{foo} = 'bar'; |
232 | |
233 | =back |
234 | |
4a6895ce |
235 | The last of these, the stash, is a universal hash for sharing data among application components. For an example, we return to our 'hello' action: |
fc7ec1d9 |
236 | |
e3dc9d78 |
237 | sub hello : Global { |
5a8ed4fe |
238 | my ( $self, $c ) = @_; |
239 | $c->stash->{message} = 'Hello World!'; |
4c6807d2 |
240 | $c->forward('show_message'); |
5a8ed4fe |
241 | } |
fc7ec1d9 |
242 | |
4c6807d2 |
243 | sub show_message : Private { |
5a8ed4fe |
244 | my ( $self, $c ) = @_; |
245 | $c->res->output( $c->stash->{message} ); |
246 | } |
fc7ec1d9 |
247 | |
dd25a192 |
248 | Note that the stash should be used only for passing data in an individual request cycle; it gets cleared at a new request. If you need to maintain more persistent data, use a session. |
249 | |
fc7ec1d9 |
250 | =head3 Actions |
251 | |
b33ed88c |
252 | A Catalyst controller is defined by its actions. An action is a sub with |
253 | a special attribute. You've already seen some examples of actions in |
254 | this document. |
cda8d1ac |
255 | |
256 | Catalyst supports several types of actions: |
fc7ec1d9 |
257 | |
258 | =over 4 |
259 | |
4a6895ce |
260 | =item * B<Literal> |
fc7ec1d9 |
261 | |
e3dc9d78 |
262 | sub bar : Path('/foo/bar') { } |
fc7ec1d9 |
263 | |
264 | Matches only http://localhost:3000/foo/bar. |
265 | |
4a6895ce |
266 | =item * B<Regex> |
fc7ec1d9 |
267 | |
b33ed88c |
268 | sub bar : Regex('^item(\d+)/order(\d+)$') { } |
fc7ec1d9 |
269 | |
b33ed88c |
270 | Matches any URL that matches the pattern in the action key, e.g. http://localhost:3000/item23/order42. The '' around the regexp is optional, but perltidy likes it. :) |
271 | |
272 | Regex matches act globally, i.e. without reference to the namespace from which it is called, so that a C<bar> method in the C<MyApp::Controller::Catalog::Order::Process> namespace won't match any form of C<bar>, C<Catalog>, C<Order>, or C<Process> unless you explicitly put this in the regex. |
fc7ec1d9 |
273 | |
72d9bfc7 |
274 | If you use capturing parentheses to extract values within the matching URL (23, 42 in the above example), those values are available in the $c->req->snippets array. If you want to pass arguments at the end of your URL, you must use regex action keys. See L</URL Argument Handling> below. |
fc7ec1d9 |
275 | |
72d9bfc7 |
276 | =item * B<Top-level> |
cda8d1ac |
277 | |
278 | package MyApp; |
279 | sub foo : Global { } |
280 | |
b33ed88c |
281 | Matches http://localhost:3000/foo. The function name is mapped directly |
282 | to the application base. |
cda8d1ac |
283 | |
4a6895ce |
284 | =item * B<Namespace-Prefixed> |
fc7ec1d9 |
285 | |
2feb6632 |
286 | package MyApp::C::My::Controller; |
e3dc9d78 |
287 | sub foo : Local { } |
fc7ec1d9 |
288 | |
cda8d1ac |
289 | Matches http://localhost:3000/my/controller/foo. |
fc7ec1d9 |
290 | |
72d9bfc7 |
291 | This action type indicates that the matching URL must be prefixed with a modified form of the component's class (package) name. This modified class name excludes the parts that have a pre-defined meaning in Catalyst ("MyApp::C" in the above example), replaces "::" with "/", and converts the name to lower case. See L</Components> for a full explanation of the pre-defined meaning of Catalyst component class names. |
fc7ec1d9 |
292 | |
4a6895ce |
293 | =item * B<Private> |
fc7ec1d9 |
294 | |
5a8ed4fe |
295 | sub foo : Private { } |
fc7ec1d9 |
296 | |
297 | Matches no URL, and cannot be executed by requesting a URL that corresponds to the action key. Private actions can be executed only inside a Catalyst application, by calling the C<forward> method: |
298 | |
5a8ed4fe |
299 | $c->forward('foo'); |
fc7ec1d9 |
300 | |
b33ed88c |
301 | See L</Flow Control> for a full explanation of C<forward>. Note that, as discussed there, when forwarding from another component, you must use the absolute path to the method, so that a private C<bar> method in your C<MyApp::Controller::Catalog::Order::Process> controller must, if called from elsewhere, be reach with C<$c-E<gt>forward('/catalog/order/process/bar')>. |
fc7ec1d9 |
302 | |
303 | =back |
304 | |
b33ed88c |
305 | B<Note:> After seeing these examples, you probably wonder what the point |
306 | is of defining names for regex and path actions. Actually, every public |
307 | action is also a private one, so you have one unified way of addressing |
308 | components in your C<forward>s. |
cda8d1ac |
309 | |
72d9bfc7 |
310 | =head4 Built-in Private Actions |
fc7ec1d9 |
311 | |
72d9bfc7 |
312 | In response to specific application states, Catalyst will automatically call these built-in private actions: |
fc7ec1d9 |
313 | |
314 | =over 4 |
315 | |
cda8d1ac |
316 | =item * B<default : Private> |
fc7ec1d9 |
317 | |
318 | Called when no other action matches. |
319 | |
cda8d1ac |
320 | =item * B<begin : Private> |
fc7ec1d9 |
321 | |
322 | Called at the beginning of a request, before any matching actions are called. |
323 | |
cda8d1ac |
324 | =item * B<end : Private> |
4a6895ce |
325 | |
326 | =back |
fc7ec1d9 |
327 | |
328 | Called at the end of a request, after all matching actions are called. |
329 | |
72d9bfc7 |
330 | =head4 B<Built-in actions in controllers/autochaining> |
fc7ec1d9 |
331 | |
cda8d1ac |
332 | Package MyApp::C::Foo; |
333 | sub begin : Private { } |
5a8ed4fe |
334 | sub default : Private { } |
fc7ec1d9 |
335 | |
72d9bfc7 |
336 | You can define the Built-in Private Actions within your controllers as |
b33ed88c |
337 | well. The actions will override the ones in lower level controllers or |
338 | your global application. |
80ef2e6d |
339 | |
72d9bfc7 |
340 | In addition to the normal built-ins, you have a special action for |
341 | making inheritance chains, 'auto'. These will be run after C<begin>, |
80ef2e6d |
342 | but before your action is processed. |
cda8d1ac |
343 | |
344 | =over 4 |
345 | |
346 | =item for a request for /foo/foo |
347 | |
348 | MyApp::begin |
80ef2e6d |
349 | MyApp::auto |
42a57832 |
350 | MyApp::C::Foo::default |
cda8d1ac |
351 | MyApp::end |
352 | |
353 | =item for a request for /foo/bar/foo |
354 | |
cda8d1ac |
355 | MyApp::C::Foo::Bar::begin |
80ef2e6d |
356 | MyApp::auto |
357 | MyApp::C::Foo::auto |
cda8d1ac |
358 | MyApp::C::Foo::Bar::default |
359 | MyApp::C::Foo::Bar::end |
80ef2e6d |
360 | |
361 | =back |
362 | |
363 | Also, if you need to break out of the chain in one of your auto |
364 | actions, you can return 0, if so, your action will not be processed, |
365 | but the end will, so for the request above, if the first auto returns |
366 | false, it would look like this: |
367 | |
368 | =over 4 |
369 | |
370 | =item for a request for /foo/bar/foo where auto returns false |
371 | |
372 | MyApp::C::Foo::Bar::begin |
373 | MyApp::auto |
374 | MyApp::C::Foo::Bar::end |
cda8d1ac |
375 | |
376 | =back |
4a6895ce |
377 | |
b33ed88c |
378 | B<Note:> auto actions have to return a true value to continue |
379 | processing! You can also die in the autochain action, in that case, the |
380 | request will go straight to the finalize stage, without processing |
03805733 |
381 | further actions. |
382 | |
383 | |
4a6895ce |
384 | =head4 B<URL Argument Handling> |
385 | |
386 | If you want to pass variable arguments at the end of a URL, you must use regex actions keys with '^' and '$' anchors, and the arguments must be separated with forward slashes (/) in the URL. For example, suppose you want to handle /foo/$bar/$baz, where $bar and $baz may vary: |
387 | |
cda8d1ac |
388 | sub foo : Regex('^foo$') { my ($self, $context, $bar, $baz) = @_; } |
4a6895ce |
389 | |
390 | But what if you also defined actions for /foo/boo and /foo/boo/hoo ? |
391 | |
e3dc9d78 |
392 | sub boo : Path('/foo/boo') { .. } |
393 | sub hoo : Path('/foo/boo/hoo') { .. } |
4a6895ce |
394 | |
395 | Catalyst matches actions in most specific to least specific order: |
396 | |
397 | /foo/boo/hoo |
398 | /foo/boo |
399 | /foo # might be /foo/bar/baz |
400 | |
cda8d1ac |
401 | So Catalyst would never mistakenly dispatch the first two URLs to the '^foo$' action. |
fc7ec1d9 |
402 | |
403 | =head3 Flow Control |
404 | |
cda8d1ac |
405 | You control the application flow with the C<forward> method, which accepts the key of an action to execute. |
fc7ec1d9 |
406 | |
e3dc9d78 |
407 | sub hello : Global { |
5a8ed4fe |
408 | my ( $self, $c ) = @_; |
409 | $c->stash->{message} = 'Hello World!'; |
4c6807d2 |
410 | $c->forward('check_message'); |
5a8ed4fe |
411 | } |
fc7ec1d9 |
412 | |
4c6807d2 |
413 | sub check_message : Private { |
5a8ed4fe |
414 | my ( $self, $c ) = @_; |
415 | return unless $c->stash->{message}; |
4c6807d2 |
416 | $c->forward('show_message'); |
5a8ed4fe |
417 | } |
fc7ec1d9 |
418 | |
4c6807d2 |
419 | sub show_message : Private { |
5a8ed4fe |
420 | my ( $self, $c ) = @_; |
421 | $c->res->output( $c->stash->{message} ); |
422 | } |
cda8d1ac |
423 | |
72d9bfc7 |
424 | As you can see from these examples, you can just use the method name as long as you are referring to methods in the same controller. If you want to forward to a method in another controller, or the main application, you will have to refer to the method by absolute path. |
cda8d1ac |
425 | |
426 | $c->forward('/my/controller/action'); |
427 | $c->forward('/default'); |
fc7ec1d9 |
428 | |
429 | You can also forward to classes and methods. |
430 | |
e3dc9d78 |
431 | sub hello : Global { |
5a8ed4fe |
432 | my ( $self, $c ) = @_; |
433 | $c->forward(qw/MyApp::M::Hello say_hello/); |
434 | } |
fc7ec1d9 |
435 | |
e3dc9d78 |
436 | sub bye : Global { |
5a8ed4fe |
437 | my ( $self, $c ) = @_; |
438 | $c->forward('MyApp::M::Hello'); |
439 | } |
fc7ec1d9 |
440 | |
441 | package MyApp::M::Hello; |
442 | |
443 | sub say_hello { |
444 | my ( $self, $c ) = @_; |
445 | $c->res->output('Hello World!'); |
446 | } |
447 | |
448 | sub process { |
449 | my ( $self, $c ) = @_; |
450 | $c->res->output('Goodbye World!'); |
451 | } |
452 | |
cda8d1ac |
453 | Note that C<forward> returns to the calling action and continues processing after the action finishes. |
fc7ec1d9 |
454 | Catalyst will automatically try to call process() if you omit the method. |
455 | |
456 | =head3 Components |
457 | |
72d9bfc7 |
458 | Catalyst has an uncommonly flexible component system. You can define as many L<Models>, L<Views>, and L<Controllers> as you like. |
fc7ec1d9 |
459 | |
4a6895ce |
460 | All components must inherit from L<Catalyst::Base>, which provides a simple class structure and some common class methods like C<config> and C<new> (constructor). |
fc7ec1d9 |
461 | |
ac4a0ae0 |
462 | package MyApp::C::Catalog; |
fc7ec1d9 |
463 | |
464 | use strict; |
465 | use base 'Catalyst::Base'; |
466 | |
467 | __PACKAGE__->config( foo => 'bar' ); |
468 | |
469 | 1; |
470 | |
72d9bfc7 |
471 | You don't have to C<use> or otherwise register Models, Views, and Controllers. Catalyst automatically discovers and instantiates them when you call C<setup> in the main application. All you need to do is put them in directories named for each Component type. Notice that you can use some very terse aliases for each one. |
fc7ec1d9 |
472 | |
473 | =over 4 |
474 | |
4a6895ce |
475 | =item * B<MyApp/Model/> |
fc7ec1d9 |
476 | |
4a6895ce |
477 | =item * B<MyApp/M/> |
fc7ec1d9 |
478 | |
4a6895ce |
479 | =item * B<MyApp/View/> |
fc7ec1d9 |
480 | |
4a6895ce |
481 | =item * B<MyApp/V/> |
fc7ec1d9 |
482 | |
4a6895ce |
483 | =item * B<MyApp/Controller/> |
fc7ec1d9 |
484 | |
4a6895ce |
485 | =item * B<MyApp/C/> |
fc7ec1d9 |
486 | |
487 | =back |
488 | |
489 | =head4 Views |
490 | |
491 | To show how to define views, we'll use an already-existing base class for the L<Template Toolkit|Template>, L<Catalyst::View::TT>. All we need to do is inherit from this class: |
492 | |
493 | package MyApp::V::TT; |
494 | |
495 | use strict; |
496 | use base 'Catalyst::View::TT'; |
497 | |
498 | 1; |
499 | |
b33ed88c |
500 | (You can also generate this automatically by using the helper script: |
501 | |
502 | script/myapp_create.pl view TT TT |
503 | |
504 | where the first C<TT> tells the script to create a Template Toolkit |
505 | view, and the second tells the script that its name should be C<TT>.) |
506 | |
fc7ec1d9 |
507 | This gives us a process() method and we can now just do $c->forward('MyApp::V::TT') to render our templates. The base class makes process() implicit, so we don't have to say C<$c-E<gt>forward(qw/MyApp::V::TT process/)>. |
508 | |
e3dc9d78 |
509 | sub hello : Global { |
5a8ed4fe |
510 | my ( $self, $c ) = @_; |
511 | $c->stash->{template} = 'hello.tt'; |
512 | } |
fc7ec1d9 |
513 | |
5a8ed4fe |
514 | sub end : Private { |
515 | my ( $self, $c ) = @_; |
2feb6632 |
516 | $c->forward('MyApp::V::TT'); |
5a8ed4fe |
517 | } |
fc7ec1d9 |
518 | |
72d9bfc7 |
519 | You normally render templates at the end of a request, so it's a perfect use for the global C<end> action. |
fc7ec1d9 |
520 | |
ba120856 |
521 | Also, be sure to put the template under the directory specified in C<$c-E<gt>config-E<gt>{root}>, or you'll be forced to look at our eyecandy debug screen. ;) |
fc7ec1d9 |
522 | |
523 | =head4 Models |
524 | |
525 | To show how to define models, again we'll use an already-existing base class, this time for L<Class::DBI>: L<Catalyst::Model::CDBI>. |
526 | |
527 | But first, we need a database. |
528 | |
529 | -- myapp.sql |
530 | CREATE TABLE foo ( |
531 | id INTEGER PRIMARY KEY, |
532 | data TEXT |
533 | ); |
534 | |
535 | CREATE TABLE bar ( |
536 | id INTEGER PRIMARY KEY, |
537 | foo INTEGER REFERENCES foo, |
538 | data TEXT |
539 | ); |
540 | |
541 | INSERT INTO foo (data) VALUES ('TEST!'); |
542 | |
543 | |
544 | % sqlite /tmp/myapp.db < myapp.sql |
545 | |
546 | Now we can create a CDBI component for this database. |
547 | |
548 | package MyApp::M::CDBI; |
549 | |
550 | use strict; |
551 | use base 'Catalyst::Model::CDBI'; |
552 | |
553 | __PACKAGE__->config( |
554 | dsn => 'dbi:SQLite:/tmp/myapp.db', |
555 | relationships => 1 |
556 | ); |
557 | |
558 | 1; |
559 | |
560 | Catalyst automatically loads table layouts and relationships. Use the stash to pass data to your templates. |
561 | |
562 | package MyApp; |
563 | |
564 | use strict; |
565 | use Catalyst '-Debug'; |
566 | |
567 | __PACKAGE__->config( |
568 | name => 'My Application', |
569 | root => '/home/joeuser/myapp/root' |
570 | ); |
cda8d1ac |
571 | |
572 | __PACKAGE__->setup; |
fc7ec1d9 |
573 | |
5a8ed4fe |
574 | sub end : Private { |
575 | my ( $self, $c ) = @_; |
576 | $c->stash->{template} ||= 'index.tt'; |
577 | $c->forward('MyApp::V::TT'); |
578 | } |
fc7ec1d9 |
579 | |
e3dc9d78 |
580 | sub view : Global { |
5a8ed4fe |
581 | my ( $self, $c, $id ) = @_; |
582 | $c->stash->{item} = MyApp::M::CDBI::Foo->retrieve($id); |
583 | } |
fc7ec1d9 |
584 | |
585 | 1; |
586 | |
587 | The id is [% item.data %] |
588 | |
589 | =head4 Controllers |
590 | |
72d9bfc7 |
591 | Multiple controllers are a good way to separate logical domains of your application. |
fc7ec1d9 |
592 | |
593 | package MyApp::C::Login; |
594 | |
72d9bfc7 |
595 | sign-in : Local { } |
596 | new-password : Local { } |
597 | sign-out : Local { } |
fc7ec1d9 |
598 | |
599 | package MyApp::C::Catalog; |
600 | |
e3dc9d78 |
601 | sub view : Local { } |
602 | sub list : Local { } |
fc7ec1d9 |
603 | |
604 | package MyApp::C::Cart; |
605 | |
e3dc9d78 |
606 | sub add : Local { } |
607 | sub update : Local { } |
608 | sub order : Local { } |
fc7ec1d9 |
609 | |
610 | =head3 Testing |
611 | |
72d9bfc7 |
612 | Catalyst has a built-in http server for testing! (Later, you can easily use a more powerful server, e.g. Apache/mod_perl, in a production environment.) |
fc7ec1d9 |
613 | |
614 | Start your application on the command line... |
615 | |
b33ed88c |
616 | script/myapp_server.pl |
fc7ec1d9 |
617 | |
618 | ...then visit http://localhost:3000/ in a browser to view the output. |
619 | |
620 | You can also do it all from the command line: |
621 | |
b33ed88c |
622 | script/myapp_test.pl http://localhost/ |
fc7ec1d9 |
623 | |
624 | Have fun! |
625 | |
3cb1db8c |
626 | =head1 SUPPORT |
627 | |
628 | IRC: |
629 | |
630 | Join #catalyst on irc.perl.org. |
631 | |
72d9bfc7 |
632 | Mailing-lists: |
3cb1db8c |
633 | |
634 | http://lists.rawmode.org/mailman/listinfo/catalyst |
635 | http://lists.rawmode.org/mailman/listinfo/catalyst-dev |
636 | |
fc7ec1d9 |
637 | =head1 AUTHOR |
638 | |
cda8d1ac |
639 | Sebastian Riedel, C<sri@oook.de> |
640 | David Naughton, C<naughton@umn.edu> |
641 | Marcus Ramberg, C<mramberg@cpan.org> |
f531dd37 |
642 | Jesse Sheidlower, C<jester@panix.com> |
fc7ec1d9 |
643 | |
644 | =head1 COPYRIGHT |
645 | |
646 | This program is free software, you can redistribute it and/or modify it under |
647 | the same terms as Perl itself. |