relative forwards call every matching private method!
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / Tutorial.pod
CommitLineData
83cea649 1=head1 NAME
2
3Catalyst::Manual::Tutorial - Getting started with Catalyst
4
5=head1 DESCRIPTION
6
7This document aims to get you up and running with Catalyst.
8
b460ad78 9NOTE: THIS DOCUMENT IS STILL VERY MUCH IN AN EARLY DRAFT STATE. SEE THE NOTES
10AT THE BOTTOM OF THE DOCUMENT.
83cea649 11
83cea649 12
13
14=head2 Installation
15
16The first step is to install Catalyst, and the simplest way to do this is to
17install the Catalyst bundle from CPAN:
18
19 $ perl -MCPAN -e 'install Bundle::Catalyst'
20
21This will retrieve Catalyst and a number of useful extensions and install them
22for you.
23
24
25=head2 Setting up your application
26
27Catalyst includes a helper script, C<catalyst.pl>, that will set up a skeleton
28application for you:
29
30 $ catalyst.pl My::App
31 created "My-App"
32 created "My-App/script"
33 created "My-App/lib"
34 created "My-App/root"
35 created "My-App/t"
36 created "My-App/t/m"
37 created "My-App/t/v"
38 created "My-App/t/c"
39 created "My-App/lib/My/App"
40 created "My-App/lib/My/App/M"
41 created "My-App/lib/My/App/V"
42 created "My-App/lib/My/App/C"
43 created "My-App/lib/My/App.pm"
44 created "My-App/Makefile.PL"
45 created "My-App/README"
46 created "My-App/Changes"
47 created "My-App/t/01app.t"
48 created "My-App/t/02podcoverage.t"
501b6190 49 created "My-App/script/cgi.pl"
50 created "My-App/script/nph-cgi.pl"
51 created "My-App/script/fcgi.pl"
83cea649 52 created "My-App/script/server.pl"
53 created "My-App/script/test.pl"
54 created "My-App/script/create.pl"
55
b460ad78 56This creates the directory structure shown, populated with skeleton files.
83cea649 57
58
59
60=head2 Testing out the sample application
61
62You can test out your new application by running the server script that
63catalyst provides:
64
65 $ cd My-App
66 $ script/server.pl
b460ad78 67 [...] [catalyst] [debug] Debug messages enabled
68 [...] [catalyst] [debug] Loaded engine "Catalyst::Engine::CGI"
69 [...] [catalyst] [debug] Initialized components ""
70 [...] [catalyst] [info] My::App powered by Catalyst 4.26
71 [...] [catalyst] [debug] "My::App" defined "!default" as "CODE(0x83fd570)"
83cea649 72 You can connect to your server at http://localhost:3000
73
b460ad78 74(Note that each line logged by Catalyst includes a timestamp, which has been
75replaced here with "C<...>" so that the text fits onto the lines.)
76
83cea649 77The server is now waiting for you to make requests of it. Try using telnet to
78manually make a simple GET request of the server (when telnet responds with
79"Escape character is '^]'.", type "GET / HTTP/1.0" and hit return twice):
80
81 $ telnet localhost 3000
82 Trying 127.0.0.1...
83 Connected to localhost.
84 Escape character is '^]'.
85 GET / HTTP/1.0
86
87 HTTP/1.0 200
88 Server: Catalyst/4.26
89 Status: 200
90 Date: Sun, 20 Mar 2005 12:31:55 GMT
91 X-catalyst: 4.26
92 Content-length: 40
93 Content-Type: text/html; charset=ISO-8859-1
94
95 Congratulations, My::App is on Catalyst!
96 Connection closed by foreign host.
97 $
98
99More trace messages will appear in the original terminal window:
100
b460ad78 101 [...] [catalyst] [debug] ********************************
102 [...] [catalyst] [debug] * Request 1 (0.027/s) [9818]
103 [...] [catalyst] [debug] ********************************
104 [...] [catalyst] [debug] "GET" request for ""
105 [...] [catalyst] [debug] Using default action
106 [...] [catalyst] [info] Processing "!default" took 0.000033s
107 [...] [catalyst] [info] Request took 0.051399s (19.456/s)
83cea649 108
109The server will continue running until you interrupt it.
110
111The application can also be tested from the command line using the generated
112helper script, C<script/test.pl>.
113
114
115=head2 Getting your application invoked
116
b460ad78 117Catalyst applications are usually run from mod_perl, but can also be run as
118CGI or FastCGI scripts. Running under mod_perl gives better performance, but
119for development purposes you may want to run your application as a CGI script,
120especially as changes to your application code take effect under CGI without
121having to restart the web server.
83cea649 122
123To run from mod_perl you need to add something like this to your Apache
124configuration file:
125
b460ad78 126 <Location /my-app>
83cea649 127 SetHandler perl-script
b460ad78 128 PerlHandler My::App
83cea649 129 </Location>
130
131To run as a CGI script you need a wrapper script like:
132
133 #!/usr/bin/perl -w
134
135 use strict;
b460ad78 136 use lib '/path/to/My/App/lib';
137 use My::App;
83cea649 138
b460ad78 139 My::App->run;
83cea649 140
141Catalyst outputs a complete HTTP response, which is not what is expected of a
142CGI script. You need to configure the script as a so-called "Non-parsed
143Headers" script for it to function properly. To do this in Apache just name
144the script starting with C<nph->.
145
b460ad78 146CHECK: is this statement still valid for Cat5?
83cea649 147
148
b460ad78 149=head2 Examining the generated code
83cea649 150
b460ad78 151The generated application code is quite simple and looks something like this:
83cea649 152
153 package My::App;
154
155 use strict;
156 use Catalyst qw/-Debug/;
157
158 our $VERSION = '0.01';
159
160 My::App->config(
161 name => 'My::App',
162 root => '/home/andrew/My-App/root',
163 );
164
165 My::App->action(
166 '!default' => sub {
167 my ( $self, $c ) = @_;
168 $c->res->output('Congratulations, My::App is on Catalyst!');
169 },
170 );
171
172 1;
173
174
b460ad78 175When the C<Catalyst> module is imported by the application code, Catalyst
176performs the first stage of its initialization. This includes loading the
177appropriate Engine module for the environment in which the application is
178running, loading any plugins and ensuring that the calling module (the
179application module) inherits from C<Catalyst> (which makes the Catalyst
180methods C<config> and C<action> available to the application module).
181
182The call to C<config> sets up configuration data for the application. The
183C<name> and C<root> items are the minimum required, and specify the name of
184the application and the path to the root directory where documents, images and
185templates can be found.
186
187Catalyst associates I<actions> with URLs and on receiving a request dispatches
188to the action that matches to request URL. The call to C<action> in the code
189above registers a default action. With just this action registered the
190application will respond to all requests with the same message
191"Congratulations, My::App is on Catalyst!".
192
193TODO: mention private actions and attributes
194
195
196The first call to the C<action> method triggers the second stage of Catalyst's
197initialization process. In this phase Catalyst searches for any component
198modules, locating and registering any actions it finds in those modules.
199Component modules have names prefixed with the application module name,
200followed by C<Model>, C<View> or C<Controller> (or the alternative short
201forms: C<M>, C<V> or C<C>) followed by the component name, for example:
202
203 My::App::Controller::ShoppingCart
204
205
206
207
208=head2 Extending the generated code
209
210NOTE: this section is outdated by Cat5.
211
83cea649 212You can start extending the application by adding new actions:
213
214 My::App->action(
215 'test1' => sub {
216 my ( $self, $c ) = @_;
217 $c->res->output('In a new test action #1');
218 },
219 'test1' => sub {
220 my ( $self, $c ) = @_;
221 $c->res->output('In a new test action #1');
222 },
223 '!default' => sub {
224 my ( $self, $c ) = @_;
225 $c->res->output('Congratulations, My::App is on Catalyst!');
226 },
227 );
228
229
b460ad78 230TODO: explain briefly about plugins, actions and components
231
232regex actions passed subexpression matches in $c->req->snippets (array ref).
233
234
235=head2 Hooking in to Template Toolkit
236
237One of the first things you will probably want to add to your application is a
238templating system for generating your output. Catalyst works well with
239Template Toolkit. If you are unfamiliar with Template Toolkit then I suggest
240you look at L<http://tt2.org>, install C<Template>, read the documentation and
241play around with it, and have a look at the I<Badger Book> (I<Template
242Toolkit> by Darren Chamberlain, Dave Cross and Andy Wardly, O'Reilly &
243Associates, 2004).
244
245You can create a stub Template Toolkit view component using the create script
246that Catalyst set up as part of the skeleton application:
247
248 $ script/create.pl view TT TT
249
250this generates a view component named C<My::App::View::TT>, which you might
251use by forwarding from your C<end> action:
252
253 # In My::App or My::App::Controller::SomeController
254
255 sub end : Private {
256 my($self, $c) = @_;
257 $c->forward('My::App::View::TT');
258 }
259
260The generated TT view component simply subclasses the C<Catalyst::View::TT>
261class. It looks like this (with the POD stripped out):
262
263 package My::App::V::TT;
264
265 use strict;
266 use base 'Catalyst::View::TT';
267
268 1;
269
270C<Catalyst::View::TT> initializes a Template Toolkit object with an options
271hash initialized with built-in default settings followed by the contents of
272the hash C<<%{__PACKAGE__->config()}>>. You can configure TT more to your
273needs by adding a C<new> method to the generated TT component:
274
275 sub new {
276 my $self = shift;
277 $self->config->{PRE_PROCESS} = 'config/main';
278 $self->config->{WRAPPER} = 'site/wrapper';
279 return $self->SUPER::new(@_);
280 }
281
83cea649 282
283
284=head1 AUTHOR
285
286Andrew Ford, C<A.Ford@ford-mason.co.uk>
287
b460ad78 288As noted above, this document is at an alpha stage. My plan for this document
289is as follows:
290
291=over 4
292
293=item 1
294
295expand this document fairly rapidly to cover topics relevant to
296a newcomer to Catalyst in an order that can be read sequentially
297
298=item 2
299
300incorporate feedback
301
302=item 3
303
304revise the text
305
306=back
307
308Placeholders are indicated by the words: TODO or CHECK
309
310Please send comments, corrections and suggestions for improvements to
311A.Ford@ford-mason.co.uk
312
313
83cea649 314=head1 COPYRIGHT
315
316This program is free software, you can redistribute it and/or modify it under
317the same terms as Perl itself.