3ed15c18cdf6f625d4bba6db64183cd4da2051ca
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / Intro.pod
1 =head1 NAME
2
3 Catalyst::Manual::Tutorial::Intro - Catalyst Tutorial - Part 1: Introduction
4
5
6 =head1 OVERVIEW
7
8 This is B<Part 1 of 10> for the Catalyst tutorial.
9
10 L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12 =over 4
13
14 =item 1
15
16 B<Introduction>
17
18 =item 2
19
20 L<Catalyst Basics|Catalyst::Manual::Tutorial::CatalystBasics>
21
22 =item 3
23
24 L<More Catalyst Basics|Catalyst::Manual::Tutorial::MoreCatalystBasics>
25
26 =item 4
27
28 L<Basic CRUD|Catalyst::Manual::Tutorial::BasicCRUD>
29
30 =item 5
31
32 L<Authentication|Catalyst::Manual::Tutorial::Authentication>
33
34 =item 6
35
36 L<Authorization|Catalyst::Manual::Tutorial::Authorization>
37
38 =item 7
39
40 L<Debugging|Catalyst::Manual::Tutorial::Debugging>
41
42 =item 8
43
44 L<Testing|Catalyst::Manual::Tutorial::Testing>
45
46 =item 9
47
48 L<Advanced CRUD|Catalyst::Manual::Tutorial::AdvancedCRUD>
49
50 =item 10
51
52 L<Appendices|Catalyst::Manual::Tutorial::Appendices>
53
54 =back
55
56
57 =head1 DESCRIPTION
58
59 This tutorial provides a multi-part introduction to the Catalyst web
60 framework. It seeks to provide a rapid overview of many of its most
61 commonly used features. The focus is on the real-world best practices
62 required in the construction of nearly all Catalyst applications.
63
64 Although the primary target of the tutorial is users new to the Catalyst
65 framework, experienced users may wish to review specific sections (for
66 example, how to use DBIC for their model classes, how to add
67 authentication and authorization to an existing application, or form
68 management).
69
70 You can obtain the code for all the tutorial examples from the
71 catalyst subversion repository by issuing the command:
72
73     svn co http://dev.catalyst.perl.org/repos/Catalyst/tags/examples/Tutorial/MyApp/5.7/ CatalystTutorial
74
75 This will download the current code for each tutorial chapter in the
76 CatalystTutorial directory.  Each example application directory has
77 the same name as the tutorial chapter.
78
79 B<These reference implementations are provided so that when you follow
80 the tutorial, you can use the code from the subversion repository to
81 ensure that your system is set up correctly, and that you have not
82 inadvertently made any typographic errors, or accidentally skipped
83 part of the tutorial.>
84
85 B<NOTE: You can use any Perl-supported OS and environment to run 
86 Catalyst.> It should make little or no difference to Catalyst's 
87 operation, B<but this tutorial has been written using Ubuntu 8.10> 
88 because that represents a quick and easy for most people to try out 
89 Catalyst with virtually zero setup time and hassles.  Also, the tutorial 
90 has been tested to work correctly with the versions of Catalyst and all 
91 the supporting modules in Ubuntu 8.10 (see "VERSIONS AND CONVENTIONS 
92 USED IN THIS TUTORIAL" below for the specific versions for some of the 
93 key modules), so B<if you think you might be running into an issue 
94 related to versions> (for example, a module changed its behavior in a 
95 newer version or a bug was introduced), B<it might be worth giving 
96 Ubuntu 8.10 a try>.  See the "CATALYST INSTALLATION" section below for 
97 more information.
98
99 If you're reading this manual online, you can download the example 
100 program and all the necessary dependencies to your local machine by 
101 installing the C<Task::Catalyst::Tutorial> distribution from CPAN:
102
103      cpan Task::Catalyst::Tutorial
104
105 This will also test to make sure the dependencies are working.  If you
106 have trouble installing these, please ask for help on the #catalyst
107 IRC channel, or the Catalyst mailing list.
108
109 Subjects covered by the tutorial include:
110
111 =over 4
112
113 =item * 
114
115 A simple application that lists and adds books.
116
117 =item *
118
119 The use of L<DBIx::Class|DBIx::Class> (DBIC) for the model.
120
121 =item * 
122
123 How to write CRUD (Create, Read, Update, and Delete) operations in
124 Catalyst.
125
126 =item *
127
128 Authentication ("auth").
129
130 =item * 
131
132 Role-based authorization ("authz").
133
134 =item * 
135
136 Attempts to provide an example showing current (5.7XXX) Catalyst
137 practices. For example, the use of 
138 L<Catalyst::Action::RenderView|Catalyst::Action::RenderView>,
139 DBIC, L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader> 
140 with C<myapp.conf>, the use of C<lib/MyApp/Controller/Root.pm> 
141 vs. C<lib/MyApp.pm>, etc.
142
143 =item * 
144
145 The use of Template Toolkit (TT).
146
147 =item * 
148
149 Useful techniques for troubleshooting and debugging Catalyst
150 applications.
151
152 =item * 
153
154 The use of SQLite as a database (with code also provided for MySQL and
155 PostgreSQL).
156
157 =item * 
158
159 The use of L<HTML::FormFu|HTML::FormFu> for automated form processing 
160 and validation.
161
162 =back
163
164 This tutorial makes the learning process its main priority.  For
165 example, the level of comments in the code found here would likely be
166 considered excessive in a "normal project."  Because of their contextual
167 value, this tutorial will generally favor inline comments over a
168 separate discussion in the text.  It also deliberately tries to
169 demonstrate multiple approaches to various features (in general, you
170 should try to be as consistent as possible with your own production
171 code).
172
173 Furthermore, this tutorial tries to minimize the number of controllers,
174 models, TT templates, and database tables.  Although this does result in
175 things being a bit contrived at times, the concepts should be applicable
176 to more complex environments.  More complete and complicated example
177 applications can be found in the C<examples> area of the Catalyst
178 Subversion repository at
179 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/>.
180
181 B<Note:> There are a variety of other introductory materials available
182 through the Catalyst web site and at
183 L<http://dev.catalyst.perl.org/wiki/UserIntroductions> and
184 L<http://dev.catalyst.perl.org/>.
185
186
187 =head1 VERSIONS AND CONVENTIONS USED IN THIS TUTORIAL
188
189 This tutorial was built using the following resources. Please note that
190 you may need to make adjustments for different environments and
191 versions:
192
193 =over 4
194
195 =item * 
196
197 Ubuntu 8.10 (Intrepid Ibex)
198
199 =item * 
200
201 Catalyst v5.7014
202
203 =item *
204
205 Catalyst::Devel v1.07
206
207 =item * 
208
209 DBIx::Class v0.08010
210
211 =item * 
212
213 Catalyst Plugins
214
215 The plugins used in this tutorial all have sufficiently stable APIs that
216 you shouldn't need to worry about versions. However, there could be
217 cases where the tutorial is affected by what version of plugins you
218 use. This tutorial has been tested against the following set of plugins:
219
220 =over 4
221
222 =item * 
223
224 Catalyst::Plugin::Authentication -- v0.10006
225
226 =item *
227
228 Catalyst::Plugin::Authorization::ACL -- v0.08
229
230 =item *
231
232 Catalyst::Plugin::Authorization::Roles -- v0.05
233
234 =item *
235
236 Catalyst::Plugin::ConfigLoader -- v0.20
237
238 =item *
239
240 Catalyst::Plugin::Session -- v0.19
241
242 =item *
243
244 Catalyst::Plugin::Session::State::Cookie -- v0.09
245
246 =item *
247
248 Catalyst::Plugin::Session::Store::FastMmap -- v0.05
249
250 =item *
251
252 Catalyst::Plugin::StackTrace -- v0.08
253
254 =item *
255
256 Catalyst::Plugin::Static::Simple -- v0.20
257
258 =back
259
260 =item * 
261
262 B<NOTE:> You can check the versions you have installed with the
263 following command:
264
265     perl -ME<lt>mod_nameE<gt> -e '"print $E<lt>mod_nameE<gt>::VERSION\n"'
266
267 For example:
268     perl -MCatalyst::Plugin::StackTrace -e 'print "$Catalyst::Plugin::StackTrace::VERSION\n"'
269
270 Since the web browser is being used on the same box where Perl and the
271 Catalyst development server is running, the URL of
272 C<http://localhost:3000> will be used (the Catalyst development server
273 defaults to port 3000).  If you are running Perl on a different box than
274 where your web browser is located (or using a different port number via
275 the C<-p> I<port_number> option to the development server), then you
276 will need to update the URL you use accordingly.
277
278 =item * 
279
280 Depending on the web browser you are using, you might need to hit 
281 C<Shift+Reload> or C<Ctrl+Reload> to pull a fresh page when testing 
282 your application at various points (see 
283 L<http://en.wikipedia.org/wiki/Bypass_your_cache> for a comprehensive
284 list of options for each browser).  Also, the C<-k> keepalive option 
285 to the development server can be necessary with some browsers 
286 (especially Internet Explorer).
287
288 =back
289
290
291 =head1 CATALYST INSTALLATION
292
293 While the rough edges of Catalyst installation have been a problem in
294 the past, this is now mostly solved.  Nonetheless, installing Catalyst
295 can be a little time consuming. Although a compelling strength of
296 Catalyst is that it makes use of many of the modules in the vast
297 repository that is CPAN, this can complicate the installation process.
298 However, there are a growing number of methods that can dramatically
299 ease this undertaking.  Of these, the following are likely to be
300 applicable to the largest number of potential new users:
301
302 =over 4
303
304 =item *
305
306 Ubuntu
307
308 Given the popularity of Ubuntu and its ease of use, Ubuntu can be a 
309 great way for newcomers to experiment with Catalyst.  Because it is a 
310 "live CD," you can simply boot from the CD, run a few commands, and you 
311 should have a fully functional environment in which to do this tutorial 
312 in a matter of minutes.  B<The tutorial was fully tested to work under 
313 Ubuntu 8.10.  Although it SHOULD work under any Catalyst installation 
314 method you might choose, it can be hard to guarantee this.>
315
316 =over 4
317
318 =item * 
319
320 Download Ubuntu 8.10 (aka, Intrepid Ibex) Desktop edition and boot from 
321 the CD and/or image file, select your language, and then "Try Ubuntu 
322 without any changes to your computer."
323
324 =item *
325
326 Open a terminal session (click "Applications" in the upper-left 
327 corner, then "Accessories," then "Terminal").
328
329 =item *
330
331 Add the 'universe' repositories:
332
333     sudo gedit /etc/apt/sources.list
334
335 And remove the comments from the lines under the comments about the
336 'universe' repositories.
337
338 =item *
339
340 Install Catalyst:
341
342     sudo apt-get update
343     sudo apt-get install libdbd-sqlite3-perl libcatalyst-perl libcatalyst-modules-perl libconfig-general-perl
344
345 Accept all of the dependencies.  Done.  
346
347 If you are running from the Live CD, you probably also want to free up 
348 some disk space with the following:
349
350     sudo apt-get clean
351
352 NOTE: While the instructions above mention the Live CD because that 
353 makes it easy for people new to Linux, you can obviously also use one 
354 of the options to install Ubuntu on your drive.
355
356 =back
357
358 =item * 
359
360 Matt Trout's C<cat-install>
361
362 Available at L<http://www.shadowcatsystems.co.uk/static/cat-install>, 
363 C<cat-install> can be a fairly painless way to get Catalyst up and 
364 running.  Just download the script from the link above and type C<perl 
365 cat-install>.  Depending on the speed of your Internet connection and 
366 your computer, it will probably take 30 to 60 minutes to install because 
367 it downloads, makes, compiles, and tests every module.  But this is an 
368 excellent way to automate the installation of all the latest modules 
369 used by Catalyst from CPAN.
370
371
372 =item * 
373
374 Other Possibilities
375
376 =over 4
377
378 =item *
379
380 OpenBSD Packages
381
382 The 2008 Advent Day 4 entry has more information on using OpenBSD 
383 packages to quickly build a system: 
384 L<http://www.catalystframework.org/calendar/2008/4>.
385
386 =item *
387
388 NetBSD Package Collection on Solaris
389
390 The 2008 Advent Day 15 entry has more information on using C<pkgsrc> and 
391 NetBSD packages on Solaris: 
392 L<http://www.catalystframework.org/calendar/2008/15|>.
393
394 =item * 
395
396 CatInABox
397
398 You can get more information at 
399 L<http://www.catalystframework.org/calendar/2008/7>
400 or L<Perl::Dist::CatInABox|Perl::Dist::CatInABox>.
401
402
403 =item *
404
405 Pre-Built VMWare Images
406
407 Under the VMWare community program, work is ongoing to develop a number
408 of VMWare images where an entire Catalyst development environment has
409 already been installed, complete with database engines and a full
410 complement of Catalyst plugins.
411
412 =item * 
413
414 Frank Speiser's Amazon EC2 Catalyst SDK
415
416 There are currently two flavors of publicly available Amazon Machine
417 Images (AMI) that include all the elements you'd need to begin
418 developing in a fully functional Catalyst environment within minutes.
419 See L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>
420 for more details.
421
422 =back
423
424 =back
425
426 For additional information and recommendations on Catalyst installation,
427 please refer to 
428 L<Catalyst::Manual::Installation|Catalyst::Manual::Installation>.
429
430 B<NOTE:> Step-by-step instructions to replicate the environment on
431 which this tutorial was developed can be found at
432 L<Catalyst::Manual::Installation::CentOS4|Catalyst::Manual::Installation::CentOS4>. 
433 Using these instructions, you should be able to build a complete CentOS
434 4.X server with Catalyst and all the plugins required to run this
435 tutorial.
436
437
438 =head1 DATABASES
439
440 This tutorial will primarily focus on SQLite because of its simplicity
441 of installation and use; however, modifications in the script required
442 to support MySQL and PostgreSQL will be presented in Appendix 2.
443
444 B<Note:> One of the advantages of the MVC design patterns is that
445 applications become much more database independent.  As such, you will
446 notice that only the C<.sql> files used to initialize the database
447 change between database systems: the Catalyst code generally remains the
448 same.
449
450
451 =head1 WHERE TO GET WORKING CODE
452
453 Each part of the tutorial has complete code available in the main
454 Catalyst Subversion repository (see the note at the beginning of each
455 part for the appropriate svn command to use).  Additionally, the final
456 code through Part 8 of the tutorial is available as a ready-to-run 
457 tarball at
458 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/Final_Tarball/MyApp_Part8.tgz>.
459 The final code for other parts of the tutorial are available at:
460 L<http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/Final_Tarballs_Per_Part/>.
461
462
463 B<NOTE:> You can run the test cases for the final code with the following 
464 commands:
465
466     wget http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial/Final_Tarball/MyApp_Part8.tgz
467     tar zxvf MyApp.tgz
468     cd MyApp
469     CATALYST_DEBUG=0 prove --lib lib  t
470
471
472 =head1 AUTHOR
473
474 Kennedy Clark, C<hkclark@gmail.com>
475
476 Please report any errors, issues or suggestions to the author.  The
477 most recent version of the Catalyst Tutorial can be found at
478 L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.70/trunk/lib/Catalyst/Manual/Tutorial/>.
479
480 Copyright 2006-2008, Kennedy Clark, under Creative Commons License
481 (L<http://creativecommons.org/licenses/by-sa/3.0/us/>).