Update for VM
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 03_MoreCatalystBasics.pod
CommitLineData
3533daff 1=head1 NAME
2
3ab6187c 3Catalyst::Manual::Tutorial::03_MoreCatalystBasics - Catalyst Tutorial - Chapter 3: More Catalyst Application Development Basics
3533daff 4
5
6=head1 OVERVIEW
7
4b4d3884 8This is B<Chapter 3 of 10> for the Catalyst tutorial.
3533daff 9
10L<Tutorial Overview|Catalyst::Manual::Tutorial>
11
12=over 4
13
14=item 1
15
3ab6187c 16L<Introduction|Catalyst::Manual::Tutorial::01_Intro>
3533daff 17
18=item 2
19
3ab6187c 20L<Catalyst Basics|Catalyst::Manual::Tutorial::02_CatalystBasics>
3533daff 21
22=item 3
23
3ab6187c 24B<03_More Catalyst Basics>
3533daff 25
26=item 4
27
3ab6187c 28L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>
3533daff 29
30=item 5
31
3ab6187c 32L<Authentication|Catalyst::Manual::Tutorial::05_Authentication>
3533daff 33
34=item 6
35
3ab6187c 36L<Authorization|Catalyst::Manual::Tutorial::06_Authorization>
3533daff 37
38=item 7
39
3ab6187c 40L<Debugging|Catalyst::Manual::Tutorial::07_Debugging>
3533daff 41
42=item 8
43
3ab6187c 44L<Testing|Catalyst::Manual::Tutorial::08_Testing>
3533daff 45
46=item 9
47
3ab6187c 48L<Advanced CRUD|Catalyst::Manual::Tutorial::09_AdvancedCRUD>
3533daff 49
50=item 10
51
3ab6187c 52L<Appendices|Catalyst::Manual::Tutorial::10_Appendices>
3533daff 53
54=back
55
56
57=head1 DESCRIPTION
58
a8f4e284 59This chapter of the tutorial builds on the work done in Chapter 2 to
60explore some features that are more typical of "real world" web
61applications. From this chapter of the tutorial onward, we will be
62building a simple book database application. Although the application
63will be too limited to be of use to anyone, it should provide a basic
64environment where we can explore a variety of features used in virtually
65all web applications.
3533daff 66
4d63a0d5 67You can check out the source code for this example from the Catalyst
68Subversion repository as per the instructions in
2217b252 69L<Catalyst::Manual::Tutorial::01_Intro>.
3533daff 70
a8f4e284 71Please take a look at
72L<Catalyst::Manual::Tutorial::01_Intro/CATALYST INSTALLATION> before
73doing the rest of this tutorial. Although the tutorial should work
74correctly under most any recent version of Perl running on any operating
b9e431e3 75system, the tutorial has been written using Debian 6 and tested to be
a8f4e284 76sure it runs correctly in this environment.
a586a09f 77
3533daff 78
79=head1 CREATE A NEW APPLICATION
80
1390ef0e 81The remainder of the tutorial will build an application called C<MyApp>.
82First use the Catalyst C<catalyst.pl> script to initialize the framework
83for the C<MyApp> application (make sure you aren't still inside the
4b4d3884 84directory of the C<Hello> application from the previous chapter of the
acbd7bdd 85tutorial or in a directory that already has a "MyApp" subdirectory):
3533daff 86
87 $ catalyst.pl MyApp
88 created "MyApp"
89 created "MyApp/script"
90 created "MyApp/lib"
91 created "MyApp/root"
92 ...
93 created "MyApp/script/myapp_create.pl"
444d6b27 94 Change to application directory and Run "perl Makefile.PL" to make sure your install is complete
c988c8b0 95
96And change the "MyApp" directory the helper created:
97
3533daff 98 $ cd MyApp
99
4b4d3884 100This creates a similar skeletal structure to what we saw in Chapter 2 of
a8f4e284 101the tutorial, except with C<MyApp> and C<myapp> substituted for C<Hello>
102and C<hello>. (As noted in Chapter 2, omit the ".pl" from the command
103if you are using Strawberry Perl.)
3533daff 104
105
106=head1 EDIT THE LIST OF CATALYST PLUGINS
107
a8f4e284 108One of the greatest benefits of Catalyst is that it has such a large
109library of bases classes and plugins available that you can use easily
110add functionality to your application. Plugins are used to seamlessly
111integrate existing Perl modules into the overall Catalyst framework. In
112general, they do this by adding additional methods to the C<context>
113object (generally written as C<$c>) that Catalyst passes to every
114component throughout the framework.
f058768a 115
c988c8b0 116Take a look at the file C<lib/MyApp.pm> that the helper created above.
3533daff 117By default, Catalyst enables three plugins/flags:
118
119=over 4
120
1390ef0e 121=item *
3533daff 122
123C<-Debug> Flag
124
125Enables the Catalyst debug output you saw when we started the
126C<script/myapp_server.pl> development server earlier. You can remove
79a529cc 127this item when you place your application into production.
3533daff 128
a8f4e284 129To be technically correct, it turns out that C<-Debug> is not a plugin,
c988c8b0 130but a I<flag>. Although most of the items specified on the C<use
131Catalyst> line of your application class will be plugins, Catalyst
132supports a limited number of flag options (of these, C<-Debug> is the
133most common). See the documentation for
134C<https://metacpan.org/module/Catalyst|Catalyst.pm> to get details on
135other flags (currently C<-Engine>, C<-Home>, C<-Log>, and C<-Stats>).
3533daff 136
444d6b27 137If you prefer, there are several other ways to enable debug output:
138
139=over 4
140
141=item *
142
c988c8b0 143Use the C<$c-E<gt>debug> method on the C<$c> Catalyst context object
444d6b27 144
145=item *
146
147The C<-d> option to C<script/myapp_server.pl>
148
149=item *
150
c988c8b0 151The C<CATALYST_DEBUG=1> environment variable (or use C<CATALYST_DEBUG=0>
152to temporarily disable debug output).
444d6b27 153
154=back
3533daff 155
a8f4e284 156B<TIP>: Depending on your needs, it can be helpful to permanently remove
157C<-Debug> from C<lib/MyApp.pm> and then use the C<-d> option to
c988c8b0 158C<script/myapp_server.pl> to re-enable it when needed. We will not be
159using that approach in the tutorial, but feel free to make use of it in
160your own projects.
3533daff 161
162=item *
163
2217b252 164L<Catalyst::Plugin::ConfigLoader>
3533daff 165
166C<ConfigLoader> provides an automatic way to load configurable
c010ae0d 167parameters for your application from a central
2217b252 168L<Config::General> file (versus having the values
a8f4e284 169hard-coded inside your Perl modules). Config::General uses syntax very
170similar to Apache configuration files. We will see how to use this
171feature of Catalyst during the authentication and authorization sections
c988c8b0 172(L<Chapter 5|Catalyst::Manual::Tutorial::05_Authentication> and
173L<Chapter 6|Catalyst::Manual::Tutorial::06_Authorization>).
a8f4e284 174
175B<IMPORTANT NOTE:> If you are using a version of
2217b252 176L<Catalyst::Devel> prior to version 1.06, be aware that
a8f4e284 177Catalyst changed the default format from YAML to the more
178straightforward C<Config::General> style. This tutorial uses the newer
179C<myapp.conf> file for C<Config::General>. However, Catalyst supports
180both formats and will automatically use either C<myapp.conf> or
181C<myapp.yml> (or any other format supported by
2217b252 182L<Catalyst::Plugin::ConfigLoader> and
183L<Config::Any>). If you are using a version of
a8f4e284 184Catalyst::Devel prior to 1.06, you can convert to the newer format by
185simply creating the C<myapp.conf> file manually and deleting
186C<myapp.yml>. The default contents of the C<myapp.conf> you create
187should only consist of one line:
1435672d 188
189 name MyApp
15e1d0b2 190
1390ef0e 191B<TIP>: This script can be useful for converting between configuration
15e1d0b2 192formats:
193
1390ef0e 194 perl -Ilib -e 'use MyApp; use Config::General;
15e1d0b2 195 Config::General->new->save_file("myapp.conf", MyApp->config);'
196
3533daff 197=item *
198
2217b252 199L<Catalyst::Plugin::Static::Simple>
3533daff 200
a8f4e284 201C<Static::Simple> provides an easy way to serve static content, such as
202images and CSS files, from the development server.
3533daff 203
204=back
205
a8f4e284 206For our application, we want to add one new plugin into the mix. To do
207this, edit C<lib/MyApp.pm> (this file is generally referred to as your
208I<application class>) and delete the lines with:
3533daff 209
1dc333c7 210 use Catalyst qw/
211 -Debug
212 ConfigLoader
213 Static::Simple
214 /;
3533daff 215
1390ef0e 216Then replace it with:
b411df01 217
acbd7bdd 218 # Load plugins
fce83e5f 219 use Catalyst qw/
1dc333c7 220 -Debug
221 ConfigLoader
222 Static::Simple
223
224 StackTrace
225 /;
1390ef0e 226
a8f4e284 227B<Note:> Recent versions of C<Catalyst::Devel> have used a variety of
228techniques to load these plugins/flags. For example, you might see the
229following:
94d8da41 230
acbd7bdd 231 __PACKAGE__->setup(qw/-Debug ConfigLoader Static::Simple/);
94d8da41 232
a8f4e284 233Don't let these variations confuse you -- they all accomplish the same
94d8da41 234result.
235
a8f4e284 236This tells Catalyst to start using one additional plugin,
237L<Catalyst::Plugin::StackTrace>, to add a stack trace to the standard
238Catalyst "debug screen" (the screen Catalyst sends to your browser when
239an error occurs). Be aware that
240L<StackTrace|Catalyst::Plugin::StackTrace> output appears in your
241browser, not in the console window from which you're running your
1390ef0e 242application, which is where logging output usually goes.
3533daff 243
444d6b27 244Make sure when adding new plugins you also include them as a new
a8f4e284 245dependency within the Makefile.PL file. For example, after adding the
246StackTrace plugin the Makefile.PL should include the following line:
3b1fa91b 247
248 requires 'Catalyst::Plugin::StackTrace';
249
250
a8f4e284 251B<Notes:>
3533daff 252
253=over 4
254
1390ef0e 255=item *
256
a8f4e284 257C<__PACKAGE__> is just a shorthand way of referencing the name of the
258package where it is used. Therefore, in C<MyApp.pm>, C<__PACKAGE__> is
259equivalent to C<MyApp>.
3533daff 260
1390ef0e 261=item *
3533daff 262
a8f4e284 263You will want to disable L<StackTrace|Catalyst::Plugin::StackTrace>
264before you put your application into production, but it can be helpful
1390ef0e 265during development.
3533daff 266
1390ef0e 267=item *
3533daff 268
a8f4e284 269When specifying plugins, you can omit C<Catalyst::Plugin::> from the
270name. Additionally, you can spread the plugin names across multiple
444d6b27 271lines as shown here or place them all on one line.
cca5cd98 272
c988c8b0 273=item *
274
275If you want to see what the StackTrace error screen looks like, edit
276C<lib/MyApp/Controller/Root.pm> and put a C<die "Oops";> command in the
277C<sub index :Path :Args(0)> method. Then start the development server
278and open C<http://localhost:3000/> in your browser. You should get a
279screen that starts with "Caught exception in
280MyApp::Controller::Root->index" with sections showing a stacktrace,
281information about the Request and Response objects, the stash (something
282we will learn about soon), the applications configuration configuration.
283B<Just don't forget to remove the die before you continue the tutorial!>
284:-)
285
3533daff 286=back
287
3533daff 288
289=head1 CREATE A CATALYST CONTROLLER
290
1390ef0e 291As discussed earlier, controllers are where you write methods that
292interact with user input. Typically, controller methods respond to
4d63a0d5 293C<GET> and C<POST> requests from the user's web browser.
3533daff 294
295Use the Catalyst C<create> script to add a controller for book-related
296actions:
297
298 $ script/myapp_create.pl controller Books
299 exists "/home/me/MyApp/script/../lib/MyApp/Controller"
300 exists "/home/me/MyApp/script/../t"
301 created "/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm"
302 created "/home/me/MyApp/script/../t/controller_Books.t"
303
c988c8b0 304Then edit C<lib/MyApp/Controller/Books.pm> (as discussed in
305L<Chapter 2|Catalyst::Manual::Tutorial::02_CatalystBasics> of
1390ef0e 306the Tutorial, Catalyst has a separate directory under C<lib/MyApp> for
307each of the three parts of MVC: C<Model>, C<View>, and C<Controller>)
308and add the following method to the controller:
3533daff 309
310 =head2 list
311
312 Fetch all book objects and pass to books/list.tt2 in stash to be displayed
313
314 =cut
1390ef0e 315
f058768a 316 sub list :Local {
3533daff 317 # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
318 # 'Context' that's used to 'glue together' the various components
319 # that make up the application
320 my ($self, $c) = @_;
321
322 # Retrieve all of the book records as book model objects and store in the
323 # stash where they can be accessed by the TT template
0ed3df53 324 # $c->stash(books => [$c->model('DB::Book')->all]);
1390ef0e 325 # But, for now, use this code until we create the model later
0ed3df53 326 $c->stash(books => '');
1390ef0e 327
3533daff 328 # Set the TT template to use. You will almost always want to do this
329 # in your action methods (action methods respond to user input in
330 # your controllers).
61cb69fd 331 $c->stash(template => 'books/list.tt2');
3533daff 332 }
333
c988c8b0 334B<TIP>: See L<Appendix 1|Catalyst::Manual::Tutorial::10_Appendices> for
335tips on removing the leading spaces when cutting and pasting example
336code from POD-based documents.
3533daff 337
a8f4e284 338Programmers experienced with object-oriented Perl should recognize
339C<$self> as a reference to the object where this method was called. On
340the other hand, C<$c> will be new to many Perl programmers who have not
c988c8b0 341used Catalyst before. This is the "Catalyst Context object", and it is
342automatically passed as the second argument to all Catalyst action
343methods. It is used to pass information between components and provide
344access to Catalyst and plugin functionality.
345
346Catalyst Controller actions are regular Perl methods, but they make use
347of attributes (the "C<:Local>" next to the "C<sub list>" in the code
348above) to provide additional information to the Catalyst dispatcher
349logic (note that there can be an optional space between the colon and
350the attribute name; you will see attributes written both ways). Most
351Catalyst Controllers use one of five action types:
0416017e 352
353=over 4
354
355=item *
356
a8f4e284 357B<:Private> -- Use C<:Private> for methods that you want to make into an
c988c8b0 358action, but you do not want Catalyst to directly expose the method to
a8f4e284 359your users. Catalyst will not map C<:Private> methods to a URI. Use
360them for various sorts of "special" methods (the C<begin>, C<auto>, etc.
361discussed below) or for methods you want to be able to C<forward> or
c988c8b0 362C<detach> to. (If the method is a "plain old method" that you
a8f4e284 363don't want to be an action at all, then just define the method without
364any attribute -- you can call it in your code, but the Catalyst
c988c8b0 365dispatcher will ignore it. You will also have to manually include
366C<$c> if you want access to the context object in the method vs. having
367Catalyst automatically include C<$c> in the argument list for you
368if it's a full-fledged action.)
0416017e 369
8fd01b0e 370There are five types of "special" built-in C<:Private> actions:
245b41d1 371C<begin>, C<end>, C<default>, C<index>, and C<auto>.
0416017e 372
26c9cad5 373=over 4
374
0416017e 375=item *
376
377With C<begin>, C<end>, C<default>, C<index> private actions, only the
378most specific action of each type will be called. For example, if you
379define a C<begin> action in your controller it will I<override> a
380C<begin> action in your application/root controller -- I<only> the
381action in your controller will be called.
382
383=item *
384
385Unlike the other actions where only a single method is called for each
386request, I<every> auto action along the chain of namespaces will be
387called. Each C<auto> action will be called I<from the application/root
388controller down through the most specific class>.
389
390=back
391
392=item *
393
a8f4e284 394B<:Path> -- C<:Path> actions let you map a method to an explicit URI
395path. For example, "C<:Path('list')>" in
396C<lib/MyApp/Controller/Books.pm> would match on the URL
397C<http://localhost:3000/books/list>, but "C<:Path('/list')>" would match
398on C<http://localhost:3000/list> (because of the leading slash). You
399can use C<:Args()> to specify how many arguments an action should
400accept. See L<Catalyst::Manual::Intro/Action_types> for more
444d6b27 401information and examples.
0416017e 402
403=item *
404
a8f4e284 405B<:Local> -- C<:Local> is merely a shorthand for
245b41d1 406"C<:Path('_name_of_method_')>". For example, these are equivalent:
c988c8b0 407"C<sub create_book :Local {...}>" and
408"C<sub create_book :Path('create_book') {...}>".
245b41d1 409
410=item *
411
a8f4e284 412B<:Global> -- C<:Global> is merely a shorthand for
245b41d1 413"C<:Path('/_name_of_method_')>". For example, these are equivalent:
a8f4e284 414"C<sub create_book :Global {...}>" and "C<sub create_book
415:Path('/create_book') {...}>".
245b41d1 416
417=item *
418
a8f4e284 419B<:Chained> -- Newer Catalyst applications tend to use the Chained
420dispatch form of action types because of its power and flexibility. It
c988c8b0 421allows a series of controller methods to be automatically dispatched
422when servicing a single user request. See
423L<Catalyst::Manual::Tutorial::04_BasicCRUD> and
424L<Catalyst::DispatchType::Chained> for more information on chained
425actions.
0416017e 426
427=back
428
c988c8b0 429You should refer to L<Catalyst::Manual::Intro/Action-types> for
a8f4e284 430additional information and for coverage of some lesser-used action types
431not discussed here (C<Regex> and C<LocalRegex>).
3533daff 432
433
434=head1 CATALYST VIEWS
435
c988c8b0 436As mentioned in L<Chapter 2|Catalyst::Manual::Tutorial::02_CatalystBasics>
437of the tutorial, views are where you render output, typically for
438display in the user's web browser (but can generate other types of
439output such as PDF or JSON). The code in C<lib/MyApp/View> selects the
440I<type> of view to use, with the actual rendering template found in the
441C<root> directory. As with virtually every aspect of Catalyst, options
442abound when it comes to the specific view technology you adopt inside
443your application. However, most Catalyst applications use the Template
444Toolkit, known as TT (for more information on TT, see
445L<http://www.template-toolkit.org>). Other somewhat popular view
446technologies include Mason (L<http://www.masonhq.com> and
447L<http://www.masonbook.com>) and L<HTML::Template>
448(L<http://html-template.sourceforge.net>).
1390ef0e 449
450
451=head2 Create a Catalyst View
3533daff 452
a8f4e284 453When using TT for the Catalyst view, the main helper script is
c988c8b0 454L<Catalyst::Helper::View::TT>. You may also come across references to
455L<Catalyst::Helper::View::TTSite>, but its use is now deprecated.
1390ef0e 456
c988c8b0 457For our book application, enter the following command to enable the
458C<TT> style of view rendering:
3533daff 459
1edbdee6 460 $ script/myapp_create.pl view HTML TT
3533daff 461 exists "/home/me/MyApp/script/../lib/MyApp/View"
462 exists "/home/me/MyApp/script/../t"
1edbdee6 463 created "/home/me/MyApp/script/../lib/MyApp/View/HTML.pm"
464 created "/home/me/MyApp/script/../t/view_HTML.t"
465
c988c8b0 466This creates a view called C<HTML> (the first argument) in a file called
467C<HTML.pm> that uses L<Catalyst::View::TT> (the second argument) as the
468"rendering engine".
469
470It is now up to you to decide how you want to structure your view
471layout. For the tutorial, we will start with a very simple TT template
472to initially demonstrate the concepts, but quickly migrate to a more
473typical "wrapper page" type of configuration (where the "wrapper"
474controls the overall "look and feel" of your site from a single file or
475set of files).
1edbdee6 476
a8f4e284 477Edit C<lib/MyApp/View/HTML.pm> and you should see something similar to
478the following:
3533daff 479
c062293d 480 __PACKAGE__->config(
481 TEMPLATE_EXTENSION => '.tt',
482 render_die => 1,
483 );
3533daff 484
1390ef0e 485And update it to match:
486
487 __PACKAGE__->config(
488 # Change default TT extension
489 TEMPLATE_EXTENSION => '.tt2',
c062293d 490 render_die => 1,
491 );
492
493This changes the default extension for Template Toolkit from '.tt' to
494'.tt2'.
495
a8f4e284 496You can also configure components in your application class. For
c988c8b0 497example, Edit C<lib/MyApp.pm> and you should see the default
498configuration above the call to C<_PACKAGE__-E<gt>setup> (your defaults
499could be different depending on the version of Catalyst you are using):
500
501 __PACKAGE__->config(
502 name => 'MyApp',
503 # Disable deprecated behavior needed by old applications
504 disable_component_resolution_regex_fallback => 1,
505 );
c062293d 506
c062293d 507
c988c8b0 508Change this to match the following:
c062293d 509
510 __PACKAGE__->config(
c988c8b0 511 name => 'MyApp',
512 # Disable deprecated behavior needed by old applications
513 disable_component_resolution_regex_fallback => 1,
514 # Configure the view
c062293d 515 'View::HTML' => {
516 #Set the location for TT files
517 INCLUDE_PATH => [
518 __PACKAGE__->path_to( 'root', 'src' ),
1390ef0e 519 ],
c062293d 520 },
1390ef0e 521 );
3533daff 522
a8f4e284 523This changes the base directory for your template files from C<root> to
524C<root/src>.
1390ef0e 525
a8f4e284 526Please stick with the settings above for the duration of the tutorial,
527but feel free to use whatever options you desire in your applications
528(as with most things Perl, there's more than one way to do it...).
1390ef0e 529
a8f4e284 530B<Note:> We will use C<root/src> as the base directory for our template
531files, with a full naming convention of
532C<root/src/_controller_name_/_action_name_.tt2>. Another popular option
533is to use C<root/> as the base (with a full filename pattern of
acbd7bdd 534C<root/_controller_name_/_action_name_.tt2>).
535
a8f4e284 536
1390ef0e 537=head2 Create a TT Template Page
3533daff 538
539First create a directory for book-related TT templates:
540
1390ef0e 541 $ mkdir -p root/src/books
3533daff 542
543Then create C<root/src/books/list.tt2> in your editor and enter:
544
c988c8b0 545 [% # This is a TT comment. -%]
3533daff 546
c988c8b0 547 [%- # Provide a title -%]
3533daff 548 [% META title = 'Book List' -%]
549
c988c8b0 550 [% # Note That the '-' at the beginning or end of TT code -%]
551 [% # "chomps" the whitespace/newline at that end of the -%]
552 [% # output (use View Source in browser to see the effect) -%]
553
554 [% # Some basic HTML with a loop to display books -%]
3533daff 555 <table>
556 <tr><th>Title</th><th>Rating</th><th>Author(s)</th></tr>
557 [% # Display each book in a table row %]
558 [% FOREACH book IN books -%]
559 <tr>
560 <td>[% book.title %]</td>
561 <td>[% book.rating %]</td>
a46b474e 562 <td></td>
3533daff 563 </tr>
564 [% END -%]
565 </table>
566
567As indicated by the inline comments above, the C<META title> line uses
1390ef0e 568TT's META feature to provide a title to the "wrapper" that we will
c988c8b0 569create later (and essentially does nothing at the moment). Meanwhile,
570the C<FOREACH> loop iterates through each C<book> model object and
571prints the C<title> and C<rating> fields.
3533daff 572
4d63a0d5 573The C<[%> and C<%]> tags are used to delimit Template Toolkit code. TT
574supports a wide variety of directives for "calling" other files,
575looping, conditional logic, etc. In general, TT simplifies the usual
444d6b27 576range of Perl operators down to the single dot (".") operator. This
4d63a0d5 577applies to operations as diverse as method calls, hash lookups, and list
c988c8b0 578index values (see L<Template::Manual::Variables> for details and
579examples). In addition to the usual L<Template> module Pod
4d63a0d5 580documentation, you can access the TT manual at
8c848468 581L<https://metacpan.org/module/Template::Manual>.
3533daff 582
a8f4e284 583B<TIP:> While you can build all sorts of complex logic into your TT
584templates, you should in general keep the "code" part of your templates
585as simple as possible. If you need more complex logic, create helper
586methods in your model that abstract out a set of code into a single call
587from your TT template. (Note that the same is true of your controller
588logic as well -- complex sections of code in your controllers should
c988c8b0 589often be pulled out and placed into your model objects.) In
590L<Chapter 4|Catalyst::Manual::Tutorial::04_BasicCRUD> of the tutorial we
591will explore some extremely helpful and powerful features of
592L<DBIx::Class> that allow you to pull code out of your views and
593controllers and place it where it rightfully belongs in a model class.
1390ef0e 594
595
596=head2 Test Run The Application
597
598To test your work so far, first start the development server:
599
f058768a 600 $ script/myapp_server.pl -r
1390ef0e 601
a8f4e284 602Then point your browser to L<http://localhost:3000> and you should still
603get the Catalyst welcome page. Next, change the URL in your browser to
604L<http://localhost:3000/books/list>. If you have everything working so
605far, you should see a web page that displays nothing other than our
606column headers for "Title", "Rating", and "Author(s)" -- we will not see
607any books until we get the database and model working below.
1390ef0e 608
609If you run into problems getting your application to run correctly, it
610might be helpful to refer to some of the debugging techniques covered in
fce83e5f 611the L<Debugging|Catalyst::Manual::Tutorial::07_Debugging> chapter of the
1390ef0e 612tutorial.
3533daff 613
614
615=head1 CREATE A SQLITE DATABASE
616
617In this step, we make a text file with the required SQL commands to
a8f4e284 618create a database table and load some sample data. We will use SQLite
619(L<http://www.sqlite.org>), a popular database that is lightweight and
620easy to use. Be sure to get at least version 3. Open C<myapp01.sql> in
621your editor and enter:
3533daff 622
623 --
624 -- Create a very simple database to hold book and author information
625 --
f058768a 626 PRAGMA foreign_keys = ON;
3b1fa91b 627 CREATE TABLE book (
3533daff 628 id INTEGER PRIMARY KEY,
629 title TEXT ,
630 rating INTEGER
631 );
3b1fa91b 632 -- 'book_author' is a many-to-many join table between books & authors
633 CREATE TABLE book_author (
b66dd084 634 book_id INTEGER REFERENCES book(id) ON DELETE CASCADE ON UPDATE CASCADE,
635 author_id INTEGER REFERENCES author(id) ON DELETE CASCADE ON UPDATE CASCADE,
3533daff 636 PRIMARY KEY (book_id, author_id)
637 );
3b1fa91b 638 CREATE TABLE author (
3533daff 639 id INTEGER PRIMARY KEY,
640 first_name TEXT,
641 last_name TEXT
642 );
643 ---
644 --- Load some sample data
645 ---
3b1fa91b 646 INSERT INTO book VALUES (1, 'CCSP SNRS Exam Certification Guide', 5);
647 INSERT INTO book VALUES (2, 'TCP/IP Illustrated, Volume 1', 5);
648 INSERT INTO book VALUES (3, 'Internetworking with TCP/IP Vol.1', 4);
649 INSERT INTO book VALUES (4, 'Perl Cookbook', 5);
650 INSERT INTO book VALUES (5, 'Designing with Web Standards', 5);
651 INSERT INTO author VALUES (1, 'Greg', 'Bastien');
652 INSERT INTO author VALUES (2, 'Sara', 'Nasseh');
653 INSERT INTO author VALUES (3, 'Christian', 'Degu');
654 INSERT INTO author VALUES (4, 'Richard', 'Stevens');
655 INSERT INTO author VALUES (5, 'Douglas', 'Comer');
656 INSERT INTO author VALUES (6, 'Tom', 'Christiansen');
657 INSERT INTO author VALUES (7, 'Nathan', 'Torkington');
658 INSERT INTO author VALUES (8, 'Jeffrey', 'Zeldman');
659 INSERT INTO book_author VALUES (1, 1);
660 INSERT INTO book_author VALUES (1, 2);
661 INSERT INTO book_author VALUES (1, 3);
662 INSERT INTO book_author VALUES (2, 4);
663 INSERT INTO book_author VALUES (3, 5);
664 INSERT INTO book_author VALUES (4, 6);
665 INSERT INTO book_author VALUES (4, 7);
666 INSERT INTO book_author VALUES (5, 8);
3533daff 667
3533daff 668Then use the following command to build a C<myapp.db> SQLite database:
669
670 $ sqlite3 myapp.db < myapp01.sql
671
672If you need to create the database more than once, you probably want to
673issue the C<rm myapp.db> command to delete the database before you use
1390ef0e 674the C<sqlite3 myapp.db E<lt> myapp01.sql> command.
3533daff 675
676Once the C<myapp.db> database file has been created and initialized, you
677can use the SQLite command line environment to do a quick dump of the
678database contents:
679
680 $ sqlite3 myapp.db
c988c8b0 681 SQLite version 3.7.3
3533daff 682 Enter ".help" for instructions
f058768a 683 Enter SQL statements terminated with a ";"
3b1fa91b 684 sqlite> select * from book;
3533daff 685 1|CCSP SNRS Exam Certification Guide|5
686 2|TCP/IP Illustrated, Volume 1|5
687 3|Internetworking with TCP/IP Vol.1|4
688 4|Perl Cookbook|5
689 5|Designing with Web Standards|5
690 sqlite> .q
691 $
692
693Or:
694
3b1fa91b 695 $ sqlite3 myapp.db "select * from book"
3533daff 696 1|CCSP SNRS Exam Certification Guide|5
697 2|TCP/IP Illustrated, Volume 1|5
698 3|Internetworking with TCP/IP Vol.1|4
699 4|Perl Cookbook|5
700 5|Designing with Web Standards|5
701
702As with most other SQL tools, if you are using the full "interactive"
703environment you need to terminate your SQL commands with a ";" (it's not
704required if you do a single SQL statement on the command line). Use
705".q" to exit from SQLite from the SQLite interactive mode and return to
706your OS command prompt.
707
a8f4e284 708Please note that here we have chosen to use 'singular' table names. This
709is because the default inflection code for older versions of
710L<DBIx::Class::Schema::Loader> does NOT handle plurals. There has been
711much philosophical discussion on whether table names should be plural or
712singular. There is no one correct answer, as long as one makes a choice
713and remains consistent with it. If you prefer plural table names (e.g.
714you think that they are easier to read) then see the documentation in
658b8c29 715L<DBIx::Class::Schema::Loader::Base/naming> (version 0.05 or greater).
3b1fa91b 716
a8f4e284 717For using other databases, such as PostgreSQL or MySQL, see
3ab6187c 718L<Appendix 2|Catalyst::Manual::Tutorial::10_Appendices>.
3533daff 719
acbd7bdd 720
8a472b34 721=head1 DATABASE ACCESS WITH DBIx::Class
3533daff 722
a8f4e284 723Catalyst can be used with virtually any form of datastore available via
c988c8b0 724Perl. For example, L<Catalyst::Model::DBI> can be used to access
725databases through the traditional Perl L<DBI> interface or you can use a
726model to access files of any type on the filesystem. However, most
727Catalyst applications use some form of object-relational mapping (ORM)
728technology to create objects associated with tables in a relational
729database, and Matt Trout's L<DBIx::Class> (abbreviated as "DBIC") is the
730usual choice (this tutorial will use L<DBIx::Class>).
a8f4e284 731
732Although DBIx::Class has included support for a C<create=dynamic> mode
733to automatically read the database structure every time the application
734starts, its use is no longer recommended. While it can make for
735"flashy" demos, the use of the C<create=static> mode we use below can be
736implemented just as quickly and provides many advantages (such as the
737ability to add your own methods to the overall DBIC framework, a
c988c8b0 738technique that we see in
739L<Chapter 4|Catalyst::Manual::Tutorial::04_BasicCRUD>).
3533daff 740
1390ef0e 741
a46b474e 742=head2 Make Sure You Have a Recent Version of the DBIx::Class Model
27909ed4 743
744First, let's be sure we have a recent version of the DBIC helper,
a8f4e284 745L<Catalyst::Model::DBIC::Schema>, so that we can take advantage of some
746recent enhancements in how foreign keys are handled with SQLite. To
747check your version, run this command:
27909ed4 748
c988c8b0 749 $ perl -MCatalyst::Model::DBIC::Schema\ 999
750 Catalyst::Model::DBIC::Schema version 999 required--this is only version 0.41.
751 BEGIN failed--compilation aborted.
3b1fa91b 752
c988c8b0 753The part we are after is the "version 0.41" at the end of the second
754line. If you are following along in Debian 6, you should have version
7550.41 or higher. If you have less than v0.39, you will need to run this
756command to install it directly from CPAN:
27909ed4 757
3e008853 758 $ cpan -i Catalyst::Model::DBIC::Schema
27909ed4 759
a8f4e284 760And re-run the version print command to verify that you are now at 0.39
761or higher.
f058768a 762
763In addition, since we are using SQLite's foreign key support here,
764please be sure that you use version C<1.27> of L<DBD::SQLite> or later:
765
c988c8b0 766 $ perl -MDBD::SQLite\ 999
767 DBD::SQLite version 999 required--this is only version 1.29.
768 BEGIN failed--compilation aborted.
f058768a 769
770Upgrade if you are not at version C<1.27> or higher.
27909ed4 771
c988c8b0 772Open C<Makefile.PL> and add the following lines to require these versions
773going forward:
774
775 requires 'Catalyst::Model::DBIC::Schema' => '0.39';
776 requires 'DBD::SQLite' => '1.27';
777
27909ed4 778
a46b474e 779=head2 Create Static DBIx::Class Schema Files
27909ed4 780
a8f4e284 781Before you continue, make sure your C<myapp.db> database file is in the
782application's topmost directory. Now use the model helper with the
783C<create=static> option to read the database with
2217b252 784L<DBIx::Class::Schema::Loader> and
27909ed4 785automatically build the required files for us:
3533daff 786
4ab6212d 787 $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
b66dd084 788 create=static dbi:SQLite:myapp.db \
789 on_connect_do="PRAGMA foreign_keys = ON"
1390ef0e 790 exists "/home/me/MyApp/script/../lib/MyApp/Model"
791 exists "/home/me/MyApp/script/../t"
27909ed4 792 Dumping manual schema for MyApp::Schema to directory /home/me/MyApp/script/../lib ...
793 Schema dump completed.
1390ef0e 794 created "/home/me/MyApp/script/../lib/MyApp/Model/DB.pm"
795 created "/home/me/MyApp/script/../t/model_DB.t"
3533daff 796
a8f4e284 797Please note the '\' above. Depending on your environment, you might be
798able to cut and paste the text as shown or need to remove the '\'
fce83e5f 799character to that the command is all on a single line.
3b1fa91b 800
27909ed4 801The C<script/myapp_create.pl> command breaks down like this:
802
803=over 4
804
805=item *
806
a8f4e284 807C<DB> is the name of the model class to be created by the helper in
c988c8b0 808the C<lib/MyApp/Model> directory.
27909ed4 809
810=item *
811
c988c8b0 812C<DBIC::Schema> is the type of the model to create. This equates to
813L<Catalyst::Model::DBIC::Schema>, the standard way to use a DBIC-based
814model inside of Catalyst.
27909ed4 815
816=item *
817
818C<MyApp::Schema> is the name of the DBIC schema file written to
819C<lib/MyApp/Schema.pm>.
820
821=item *
822
c988c8b0 823C<create=static> causes L<DBIx::Class::Schema::Loader> to load the
824schema as it runs and then write that information out into
825C<lib/MyApp/Schema.pm> and files under the C<lib/MyApp/Schema>
826directory.
27909ed4 827
828=item *
829
a8f4e284 830C<dbi:SQLite:myapp.db> is the standard DBI connect string for use with
831SQLite.
27909ed4 832
f058768a 833=item *
834
a8f4e284 835And finally, the C<on_connect_do> string requests that
2217b252 836L<DBIx::Class::Schema::Loader> create
a8f4e284 837foreign key relationships for us (this is not needed for databases such
838as PostgreSQL and MySQL, but is required for SQLite). If you take a look
839at C<lib/MyApp/Model/DB.pm>, you will see that the SQLite pragma is
c988c8b0 840propagated to the Model, so that SQLite's recent (and optional) foreign
a8f4e284 841key enforcement is enabled at the start of every database connection.
f058768a 842
843
27909ed4 844=back
845
a8f4e284 846If you look in the C<lib/MyApp/Schema.pm> file, you will find that it
847only contains a call to the C<load_namespaces> method. You will also
848find that C<lib/MyApp> contains a C<Schema> subdirectory, which then has
849a subdirectory called "Result". This "Result" subdirectory then has
850files named according to each of the tables in our simple database
851(C<Author.pm>, C<BookAuthor.pm>, and C<Book.pm>). These three files are
c988c8b0 852called "Result Classes" (or
853"L<ResultSource Classes|DBIx::Class::ResultSource>") in DBIx::Class
854nomenclature. Although the Result Class files are named after tables in
855our database, the classes correspond to the I<row-level data> that is
856returned by DBIC (more on this later, especially in
3ab6187c 857L<Catalyst::Manual::Tutorial::04_BasicCRUD/EXPLORING THE POWER OF DBIC>).
27909ed4 858
a8f4e284 859The idea with the Result Source files created under
860C<lib/MyApp/Schema/Result> by the C<create=static> option is to only
861edit the files below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!>
862warning. If you place all of your changes below that point in the file,
863you can regenerate the automatically created information at the top of
864each file should your database structure get updated.
865
866Also note the "flow" of the model information across the various files
867and directories. Catalyst will initially load the model from
868C<lib/MyApp/Model/DB.pm>. This file contains a reference to
869C<lib/MyApp/Schema.pm>, so that file is loaded next. Finally, the call
870to C<load_namespaces> in C<Schema.pm> will load each of the "Result
871Class" files from the C<lib/MyApp/Schema/Result> subdirectory. The
872final outcome is that Catalyst will dynamically create three
873table-specific Catalyst models every time the application starts (you
874can see these three model files listed in the debug output generated
27909ed4 875when you launch the application).
876
c988c8b0 877Additionally, the C<lib/MyApp/Schema.pm> model can easily be loaded
878outside of Catalyst, for example, in command-line utilities and/or cron
879jobs. C<lib/MyApp/Model/DB.pm> provides a very thin "bridge" between
880Catalyst this external database model. Once you see how we can add some
881powerful features to our DBIC model in
882L<Chapter 4|Catalyst::Manual::Tutorial::04_BasicCRUD>, the elegance
883of this approach will start to become more obvious.
884
a8f4e284 885B<NOTE:> Older versions of
2217b252 886L<Catalyst::Model::DBIC::Schema> use the
a8f4e284 887deprecated DBIx::Class C<load_classes> technique instead of the newer
888C<load_namespaces>. For new applications, please try to use
889C<load_namespaces> since it more easily supports a very useful DBIC
890technique called "ResultSet Classes." If you need to convert an
891existing application from "load_classes" to "load_namespaces," you can
b66dd084 892use this process to automate the migration, but first make sure you have
893version C<0.39> of L<Catalyst::Model::DBIC::Schema> and
894L<DBIx::Class::Schema::Loader> version C<0.05000> or later.
27909ed4 895
b66dd084 896 $ # Re-run the helper to upgrade for you
27909ed4 897 $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
b66dd084 898 create=static naming=current use_namespaces=1 \
899 dbi:SQLite:myapp.db \
900 on_connect_do="PRAGMA foreign_keys = ON"
dc9a0503 901
f058768a 902
1390ef0e 903=head1 ENABLE THE MODEL IN THE CONTROLLER
904
a8f4e284 905Open C<lib/MyApp/Controller/Books.pm> and un-comment the model code we
acbd7bdd 906left disabled earlier so that your version matches the following (un-
a8f4e284 907comment the line containing C<[$c-E<gt>model('DB::Book')-E<gt>all]> and
908delete the next 2 lines):
1390ef0e 909
910 =head2 list
911
912 Fetch all book objects and pass to books/list.tt2 in stash to be displayed
913
914 =cut
915
f058768a 916 sub list :Local {
1390ef0e 917 # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
918 # 'Context' that's used to 'glue together' the various components
919 # that make up the application
920 my ($self, $c) = @_;
921
f058768a 922 # Retrieve all of the book records as book model objects and store
923 # in the stash where they can be accessed by the TT template
0ed3df53 924 $c->stash(books => [$c->model('DB::Book')->all]);
1390ef0e 925
926 # Set the TT template to use. You will almost always want to do this
927 # in your action methods (action methods respond to user input in
928 # your controllers).
61cb69fd 929 $c->stash(template => 'books/list.tt2');
1390ef0e 930 }
931
a8f4e284 932B<TIP>: You may see the C<$c-E<gt>model('DB::Book')> un-commented above
933written as C<$c-E<gt>model('DB')-E<gt>resultset('Book')>. The two are
934equivalent. Either way, C<$c-E<gt>model> returns a
2217b252 935L<DBIx::Class::ResultSet> which handles queries
a8f4e284 936against the database and iterating over the set of results that is
c93b5eaa 937returned.
938
a8f4e284 939We are using the C<-E<gt>all> to fetch all of the books. DBIC supports
940a wide variety of more advanced operations to easily do things like
941filtering and sorting the results. For example, the following could be
942used to sort the results by descending title:
c93b5eaa 943
3b1fa91b 944 $c->model('DB::Book')->search({}, {order_by => 'title DESC'});
c93b5eaa 945
a8f4e284 946Some other examples are provided in
947L<DBIx::Class::Manual::Cookbook/Complex WHERE clauses>, with additional
948information found at L<DBIx::Class::ResultSet/search>,
c988c8b0 949L<DBIx::Class::Manual::FAQ/Searching>, L<DBIx::Class::Manual::Intro> and
2217b252 950L<Catalyst::Model::DBIC::Schema>.
1390ef0e 951
952
953=head2 Test Run The Application
3533daff 954
c988c8b0 955First, let's enable an environment variable that causes L<DBIx::Class>
956to dump the SQL statements used to access the database. This is a
957helpful trick when you are trying to debug your database-oriented code.
958Press C<Ctrl-C> to break out of the development server and enter:
3533daff 959
960 $ export DBIC_TRACE=1
c988c8b0 961 $ script/myapp_server.pl -r
f33d1dd7 962
4d63a0d5 963This assumes you are using bash as your shell -- adjust accordingly if
3533daff 964you are using a different shell (for example, under tcsh, use
965C<setenv DBIC_TRACE 1>).
966
d0496197 967B<NOTE:> You can also set this in your code using
3533daff 968C<$class-E<gt>storage-E<gt>debug(1);>. See
969L<DBIx::Class::Manual::Troubleshooting> for details (including options
a8f4e284 970to log to a file instead of displaying to the Catalyst development
971server log).
3533daff 972
1390ef0e 973Then launch the Catalyst development server. The log output should
974display something like:
3533daff 975
f058768a 976 $ script/myapp_server.pl -r
3533daff 977 [debug] Debug messages enabled
1390ef0e 978 [debug] Statistics enabled
3533daff 979 [debug] Loaded plugins:
980 .----------------------------------------------------------------------------.
a467a714 981 | Catalyst::Plugin::ConfigLoader 0.27 |
982 | Catalyst::Plugin::StackTrace 0.11 |
3533daff 983 '----------------------------------------------------------------------------'
984
985 [debug] Loaded dispatcher "Catalyst::Dispatcher"
986 [debug] Loaded engine "Catalyst::Engine::HTTP"
987 [debug] Found home "/home/me/MyApp"
45d511e0 988 [debug] Loaded Config "/home/me/MyApp/myapp.conf"
3533daff 989 [debug] Loaded components:
990 .-----------------------------------------------------------------+----------.
991 | Class | Type |
992 +-----------------------------------------------------------------+----------+
993 | MyApp::Controller::Books | instance |
994 | MyApp::Controller::Root | instance |
d0496197 995 | MyApp::Model::DB | instance |
3b1fa91b 996 | MyApp::Model::DB::Author | class |
997 | MyApp::Model::DB::Book | class |
998 | MyApp::Model::DB::BookAuthor | class |
1edbdee6 999 | MyApp::View::HTML | instance |
3533daff 1000 '-----------------------------------------------------------------+----------'
1001
1002 [debug] Loaded Private actions:
1003 .----------------------+--------------------------------------+--------------.
1004 | Private | Class | Method |
1005 +----------------------+--------------------------------------+--------------+
1006 | /default | MyApp::Controller::Root | default |
1007 | /end | MyApp::Controller::Root | end |
1390ef0e 1008 | /index | MyApp::Controller::Root | index |
3533daff 1009 | /books/index | MyApp::Controller::Books | index |
1010 | /books/list | MyApp::Controller::Books | list |
1011 '----------------------+--------------------------------------+--------------'
1012
1013 [debug] Loaded Path actions:
1014 .-------------------------------------+--------------------------------------.
1015 | Path | Private |
1016 +-------------------------------------+--------------------------------------+
1390ef0e 1017 | / | /default |
1018 | / | /index |
1019 | /books | /books/index |
3533daff 1020 | /books/list | /books/list |
1021 '-------------------------------------+--------------------------------------'
1022
f058768a 1023 [info] MyApp powered by Catalyst 5.80020
acbd7bdd 1024 You can connect to your server at http://debian:3000
3533daff 1025
a8f4e284 1026B<NOTE:> Be sure you run the C<script/myapp_server.pl> command from the
1027'base' directory of your application, not inside the C<script> directory
1028itself or it will not be able to locate the C<myapp.db> database file.
1029You can use a fully qualified or a relative path to locate the database
1030file, but we did not specify that when we ran the model helper earlier.
3533daff 1031
1032Some things you should note in the output above:
1033
1034=over 4
1035
1390ef0e 1036=item *
3533daff 1037
c988c8b0 1038L<Catalyst::Model::DBIC::Schema> dynamically created three model
1039classes, one to represent each of the three tables in our database
a8f4e284 1040(C<MyApp::Model::DB::Author>, C<MyApp::Model::DB::BookAuthor>, and
1041C<MyApp::Model::DB::Book>).
3533daff 1042
1390ef0e 1043=item *
3533daff 1044
1045The "list" action in our Books controller showed up with a path of
1046C</books/list>.
1047
1048=back
1049
1050Point your browser to L<http://localhost:3000> and you should still get
1051the Catalyst welcome page.
1052
1053Next, to view the book list, change the URL in your browser to
1054L<http://localhost:3000/books/list>. You should get a list of the five
1390ef0e 1055books loaded by the C<myapp01.sql> script above without any formatting.
1056The rating for each book should appear on each row, but the "Author(s)"
191dee29 1057column will still be blank (we will fill that in later).
3533daff 1058
a8f4e284 1059Also notice in the output of the C<script/myapp_server.pl> that
c988c8b0 1060L<DBIx::Class> used the following SQL to retrieve the data:
3533daff 1061
fce83e5f 1062 SELECT me.id, me.title, me.rating FROM book me
3533daff 1063
1064because we enabled DBIC_TRACE.
1065
0c51850e 1066You now have the beginnings of a simple but workable web application.
a8f4e284 1067Continue on to future sections and we will develop the application more
1068fully.
3533daff 1069
1070
1390ef0e 1071=head1 CREATE A WRAPPER FOR THE VIEW
1072
a8f4e284 1073When using TT, you can (and should) create a wrapper that will literally
1074wrap content around each of your templates. This is certainly useful as
1075you have one main source for changing things that will appear across
1076your entire site/application instead of having to edit many individual
1077files.
1390ef0e 1078
1079
1edbdee6 1080=head2 Configure HTML.pm For The Wrapper
1390ef0e 1081
a8f4e284 1082In order to create a wrapper, you must first edit your TT view and tell
1083it where to find your wrapper file.
1390ef0e 1084
a8f4e284 1085Edit your TT view in C<lib/MyApp/View/HTML.pm> and change it to match
1086the following:
1390ef0e 1087
1088 __PACKAGE__->config(
1089 # Change default TT extension
1090 TEMPLATE_EXTENSION => '.tt2',
1091 # Set the location for TT files
1092 INCLUDE_PATH => [
c2dfb562 1093 MyApp->path_to( 'root', 'src' ),
1390ef0e 1094 ],
1095 # Set to 1 for detailed timer stats in your HTML as comments
1096 TIMER => 0,
1097 # This is your wrapper template located in the 'root/src'
1098 WRAPPER => 'wrapper.tt2',
1099 );
1100
1101
1102=head2 Create the Wrapper Template File and Stylesheet
1103
1104Next you need to set up your wrapper template. Basically, you'll want
a8f4e284 1105to take the overall layout of your site and put it into this file. For
1106the tutorial, open C<root/src/wrapper.tt2> and input the following:
1390ef0e 1107
1108 <?xml version="1.0" encoding="UTF-8"?>
c988c8b0 1109 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" [%#
1110 %]"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
1390ef0e 1111 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
1112 <head>
1113 <title>[% template.title or "My Catalyst App!" %]</title>
1114 <link rel="stylesheet" href="[% c.uri_for('/static/css/main.css') %]" />
1115 </head>
1116
1117 <body>
1118 <div id="outer">
1119 <div id="header">
1120 [%# Your logo could go here -%]
1121 <img src="[% c.uri_for('/static/images/btn_88x31_powered.png') %]" />
1122 [%# Insert the page title -%]
1123 <h1>[% template.title or site.title %]</h1>
1124 </div>
1125
1126 <div id="bodyblock">
1127 <div id="menu">
1128 Navigation:
1129 <ul>
1130 <li><a href="[% c.uri_for('/books/list') %]">Home</a></li>
c988c8b0 1131 <li><a href="[% c.uri_for('/')
1132 %]" title="Catalyst Welcome Page">Welcome</a></li>
1390ef0e 1133 </ul>
1134 </div><!-- end menu -->
1135
1136 <div id="content">
1137 [%# Status and error messages %]
1138 <span class="message">[% status_msg %]</span>
1139 <span class="error">[% error_msg %]</span>
1140 [%# This is where TT will stick all of your template's contents. -%]
1141 [% content %]
1142 </div><!-- end content -->
1143 </div><!-- end bodyblock -->
1144
1145 <div id="footer">Copyright (c) your name goes here</div>
c2dfb562 1146 </div><!-- end outer -->
1390ef0e 1147
1148 </body>
1149 </html>
1150
1151Notice the status and error message sections in the code above:
1152
1153 <span class="status">[% status_msg %]</span>
1154 <span class="error">[% error_msg %]</span>
1155
1156If we set either message in the Catalyst stash (e.g.,
a8f4e284 1157C<$c-E<gt>stash-E<gt>{status_msg} = 'Request was successful!'>) it will
1158be displayed whenever any view used by that request is rendered. The
1159C<message> and C<error> CSS styles can be customized to suit your needs
1160in the C<root/static/css/main.css> file we create below.
1390ef0e 1161
a8f4e284 1162B<Notes:>
1390ef0e 1163
1164=over 4
1165
1166=item *
1167
a8f4e284 1168The Catalyst stash only lasts for a single HTTP request. If you need to
1169retain information across requests you can use
c988c8b0 1170L<Catalyst::Plugin::Session> (we will use Catalyst sessions in the
1171Authentication chapter of the tutorial).
1390ef0e 1172
1173=item *
1174
a8f4e284 1175Although it is beyond the scope of this tutorial, you may wish to use a
1176JavaScript or AJAX tool such as jQuery (L<http://www.jquery.com>) or
1390ef0e 1177Dojo (L<http://www.dojotoolkit.org>).
1178
1179=back
1180
1181
1182=head3 Create A Basic Stylesheet
1183
1184First create a central location for stylesheets under the static
1185directory:
1186
1187 $ mkdir root/static/css
1188
1189Then open the file C<root/static/css/main.css> (the file referenced in
1190the stylesheet href link of our wrapper above) and add the following
1191content:
1192
1193 #header {
1194 text-align: center;
1195 }
1196 #header h1 {
1197 margin: 0;
1198 }
1199 #header img {
1200 float: right;
1201 }
1202 #footer {
1203 text-align: center;
1204 font-style: italic;
1205 padding-top: 20px;
1206 }
1207 #menu {
1208 font-weight: bold;
1209 background-color: #ddd;
1210 }
1211 #menu ul {
1212 list-style: none;
1213 float: left;
1214 margin: 0;
1215 padding: 0 0 50% 5px;
1216 font-weight: normal;
1217 background-color: #ddd;
1218 width: 100px;
1219 }
1220 #content {
1221 margin-left: 120px;
1222 }
1223 .message {
1224 color: #390;
1225 }
1226 .error {
1227 color: #f00;
1228 }
1229
1230You may wish to check out a "CSS Framework" like Emastic
a8f4e284 1231(L<http://code.google.com/p/emastic/>) as a way to quickly provide lots
1232of high-quality CSS functionality.
1390ef0e 1233
1234
1235=head2 Test Run The Application
1236
a8f4e284 1237Hit "Reload" in your web browser and you should now see a formatted
1238version of our basic book list. (Again, the development server should
1239have automatically restarted when you made changes to
1240C<lib/MyApp/View/HTML.pm>. If you are not using the "-r" option, you
1241will need to hit C<Ctrl-C> and manually restart it. Also note that the
1242development server does I<NOT> need to restart for changes to the TT and
1243static files we created and edited in the C<root> directory -- those
1244updates are handled on a per-request basis.)
f058768a 1245
a8f4e284 1246Although our wrapper and stylesheet are obviously very simple, you
1247should see how it allows us to control the overall look of an entire
1248website from two central files. To add new pages to the site, just
1249provide a template that fills in the C<content> section of our wrapper
1250template -- the wrapper will provide the overall feel of the page.
1390ef0e 1251
1252
a46b474e 1253=head2 Updating the Generated DBIx::Class Result Class Files
3533daff 1254
a8f4e284 1255If you take a look at the Schema files automatically generated by
1256L<DBIx::Class::Schema::Loader>, you will see that it has already defined
1257C<has_many> and C<belongs_to> relationships on each side of our foreign
1258keys. For example, take a look at C<lib/MyApp/Schema/Result/Book.pm> and
1259notice the following code:
f058768a 1260
1261 =head1 RELATIONS
c6b49844 1262
f058768a 1263 =head2 book_authors
c6b49844 1264
f058768a 1265 Type: has_many
c6b49844 1266
f058768a 1267 Related object: L<MyApp::Schema::Result::BookAuthor>
c6b49844 1268
f058768a 1269 =cut
c6b49844 1270
f058768a 1271 __PACKAGE__->has_many(
1272 "book_authors",
1273 "MyApp::Schema::Result::BookAuthor",
1274 { "foreign.book_id" => "self.id" },
c988c8b0 1275 { cascade_copy => 0, cascade_delete => 0 },
f058768a 1276 );
1277
a8f4e284 1278Each C<Book> "has_many" C<book_authors>, where C<BookAuthor> is the
1279many-to-many table that allows each Book to have multiple Authors, and
c988c8b0 1280each Author to have multiple books. The arguments to C<has_many> are:
f058768a 1281
1282=over 4
1283
1284=item *
1285
a8f4e284 1286C<book_authors> - The name for this relationship. DBIC will create an
1287accessor on the C<Books> DBIC Row object with this name.
f058768a 1288
1289=item *
1290
a8f4e284 1291C<MyApp::Schema::Result::BookAuthor> - The name of the DBIC model class
1292referenced by this C<has_many> relationship.
f058768a 1293
1294=item *
1295
a8f4e284 1296C<foreign.book_id> - C<book_id> is the name of the foreign key column in
1297the I<foreign> table that points back to this table.
f058768a 1298
1299=item *
1300
a8f4e284 1301C<self.id> - C<id> is the name of the column in I<this> table that is
1302referenced by the foreign key.
f058768a 1303
1304=back
1305
a8f4e284 1306See L<DBIx::Class::Relationship/has_many> for additional information.
1307Note that you might see a "hand coded" version of the C<has_many>
1308relationship above expressed as:
f058768a 1309
1310 __PACKAGE__->has_many(
1311 "book_authors",
1312 "MyApp::Schema::Result::BookAuthor",
1313 "book_id",
1314 );
1315
a8f4e284 1316Where the third argument is simply the name of the column in the foreign
1317table. However, the hashref syntax used by
1318L<DBIx::Class::Schema::Loader> is more flexible (for example, it can
c988c8b0 1319handle "multi-column foreign keys").
f058768a 1320
a8f4e284 1321B<Note:> If you are using older versions of SQLite and related DBIC
1322tools, you will need to manually define your C<has_many> and
1323C<belongs_to> relationships. We recommend upgrading to the versions
1324specified above. :-)
f058768a 1325
a8f4e284 1326Have a look at C<lib/MyApp/Schema/Result/BookAuthor.pm> and notice that
1327there is a C<belongs_to> relationship defined that acts as the "mirror
1328image" to the C<has_many> relationship we just looked at above:
f058768a 1329
1330 =head1 RELATIONS
c6b49844 1331
f058768a 1332 =head2 book
c6b49844 1333
f058768a 1334 Type: belongs_to
c6b49844 1335
f058768a 1336 Related object: L<MyApp::Schema::Result::Book>
c6b49844 1337
f058768a 1338 =cut
c6b49844 1339
f058768a 1340 __PACKAGE__->belongs_to(
1341 "book",
1342 "MyApp::Schema::Result::Book",
1343 { id => "book_id" },
c988c8b0 1344 { join_type => "LEFT", on_delete => "CASCADE", on_update => "CASCADE" },
f058768a 1345 );
1346
a8f4e284 1347The arguments are similar, but see
f058768a 1348L<DBIx::Class::Relationship/belongs_to> for the details.
f33d1dd7 1349
a8f4e284 1350Although recent versions of SQLite and L<DBIx::Class::Schema::Loader>
1351automatically handle the C<has_many> and C<belongs_to> relationships,
7040a6cd 1352C<many_to_many> relationship bridges (not technically a relationship)
a8f4e284 1353currently need to be manually inserted. To add a C<many_to_many>
1354relationship bridge, first edit C<lib/MyApp/Schema/Result/Book.pm> and
1355add the following text below the C<# You can replace this text...>
1356comment:
3533daff 1357
3533daff 1358 # many_to_many():
1359 # args:
7040a6cd 1360 # 1) Name of relationship bridge, DBIC will create accessor with this name
1390ef0e 1361 # 2) Name of has_many() relationship this many_to_many() is shortcut for
1362 # 3) Name of belongs_to() relationship in model class of has_many() above
3533daff 1363 # You must already have the has_many() defined to use a many_to_many().
fce83e5f 1364 __PACKAGE__->many_to_many(authors => 'book_authors', 'author');
3533daff 1365
a8f4e284 1366B<Note:> Be careful to put this code I<above> the C<1;> at the end of
1367the file. As with any Perl package, we need to end the last line with a
1368statement that evaluates to C<true>. This is customarily done with
3533daff 1369C<1;> on a line by itself.
1370
7040a6cd 1371The C<many_to_many> relationship bridge is optional, but it makes it
a8f4e284 1372easier to map a book to its collection of authors. Without it, we would
1373have to "walk" through the C<book_author> table as in
1374C<$book-E<gt>book_author-E<gt>first-E<gt>author-E<gt>last_name> (we will
1375see examples on how to use DBIx::Class objects in your code soon, but
1376note that because C<$book-E<gt>book_author> can return multiple authors,
1377we have to use C<first> to display a single author). C<many_to_many>
1378allows us to use the shorter
1379C<$book-E<gt>author-E<gt>first-E<gt>last_name>. Note that you cannot
1380define a C<many_to_many> relationship bridge without also having the
5a82cb36 1381C<has_many> relationship in place.
3533daff 1382
a8f4e284 1383Then edit C<lib/MyApp/Schema/Result/Author.pm> and add the reverse
1384C<many_to_many> relationship bridge for C<Author> as follows (again, be
1385careful to put in above the C<1;> but below the C<# DO NOT MODIFY THIS
1386OR ANYTHING ABOVE!> comment):
3533daff 1387
3533daff 1388 # many_to_many():
1389 # args:
7040a6cd 1390 # 1) Name of relationship bridge, DBIC will create accessor with this name
3533daff 1391 # 2) Name of has_many() relationship this many_to_many() is shortcut for
1390ef0e 1392 # 3) Name of belongs_to() relationship in model class of has_many() above
3533daff 1393 # You must already have the has_many() defined to use a many_to_many().
fce83e5f 1394 __PACKAGE__->many_to_many(books => 'book_authors', 'book');
3533daff 1395
f058768a 1396
1390ef0e 1397=head2 Run The Application
3533daff 1398
4d63a0d5 1399Run the Catalyst development server script with the C<DBIC_TRACE> option
1400(it might still be enabled from earlier in the tutorial, but here is an
f33d1dd7 1401alternate way to specify the trace option just in case):
3533daff 1402
f058768a 1403 $ DBIC_TRACE=1 script/myapp_server.pl -r
3533daff 1404
1390ef0e 1405Make sure that the application loads correctly and that you see the
a8f4e284 1406three dynamically created model class (one for each of the Result
1407Classes we created).
3533daff 1408
a8f4e284 1409Then hit the URL L<http://localhost:3000/books/list> with your browser
f33d1dd7 1410and be sure that the book list still displays correctly.
3533daff 1411
c988c8b0 1412B<Note:> You will not see the authors yet because the view isn't taking
1413advantage of these relationships. Read on to the next section where we
1414update the template to do that.
3533daff 1415
1416
1417=head1 UPDATING THE VIEW
1418
a8f4e284 1419Let's add a new column to our book list page that takes advantage of the
1420relationship information we manually added to our schema files in the
1421previous section. Edit C<root/src/books/list.tt2> and replace the
1422"empty" table cell "<td></td>" with the following:
3533daff 1423
acbd7bdd 1424 ...
3533daff 1425 <td>
fce83e5f 1426 [% # NOTE: See Chapter 4 for a better way to do this! -%]
3533daff 1427 [% # First initialize a TT variable to hold a list. Then use a TT FOREACH -%]
1428 [% # loop in 'side effect notation' to load just the last names of the -%]
6d97b973 1429 [% # authors into the list. Note that the 'push' TT vmethod doesn't return -%]
3533daff 1430 [% # a value, so nothing will be printed here. But, if you have something -%]
6d97b973 1431 [% # in TT that does return a value and you don't want it printed, you -%]
1432 [% # 1) assign it to a bogus value, or -%]
1433 [% # 2) use the CALL keyword to call it and discard the return value. -%]
3533daff 1434 [% tt_authors = [ ];
1435 tt_authors.push(author.last_name) FOREACH author = book.authors %]
1436 [% # Now use a TT 'virtual method' to display the author count in parens -%]
1437 [% # Note the use of the TT filter "| html" to escape dangerous characters -%]
1438 ([% tt_authors.size | html %])
1439 [% # Use another TT vmethod to join & print the names & comma separators -%]
1440 [% tt_authors.join(', ') | html %]
1441 </td>
acbd7bdd 1442 ...
3533daff 1443
a8f4e284 1444B<IMPORTANT NOTE:> Again, you should keep as much "logic code" as
1445possible out of your views. This kind of logic belongs in your model
1446(the same goes for controllers -- keep them as "thin" as possible and
1447push all of the "complicated code" out to your model objects). Avoid
1448code like you see in the previous example -- we are only using it here
1449to show some extra features in TT until we get to the more advanced
1450model features we will see in Chapter 4 (see
fce83e5f 1451L<Catalyst::Manual::Tutorial::04_BasicCRUD/EXPLORING THE POWER OF DBIC>).
1452
a8f4e284 1453Then hit "Reload" in your browser (note that you don't need to reload
1454the development server or use the C<-r> option when updating TT
1455templates) and you should now see the number of authors each book has
1456along with a comma-separated list of the authors' last names. (If you
1457didn't leave the development server running from the previous step, you
1458will obviously need to start it before you can refresh your browser
1459window.)
1390ef0e 1460
1461If you are still running the development server with C<DBIC_TRACE>
1462enabled, you should also now see five more C<SELECT> statements in the
1463debug output (one for each book as the authors are being retrieved by
a46b474e 1464DBIx::Class):
3533daff 1465
fce83e5f 1466 SELECT me.id, me.title, me.rating FROM book me:
3b1fa91b 1467 SELECT author.id, author.first_name, author.last_name FROM book_author me
fce83e5f 1468 JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '1'
3b1fa91b 1469 SELECT author.id, author.first_name, author.last_name FROM book_author me
fce83e5f 1470 JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '2'
3b1fa91b 1471 SELECT author.id, author.first_name, author.last_name FROM book_author me
fce83e5f 1472 JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '3'
3b1fa91b 1473 SELECT author.id, author.first_name, author.last_name FROM book_author me
fce83e5f 1474 JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '4'
3b1fa91b 1475 SELECT author.id, author.first_name, author.last_name FROM book_author me
fce83e5f 1476 JOIN author author ON author.id = me.author_id WHERE ( me.book_id = ? ): '5'
c2dfb562 1477
a8f4e284 1478Also note in C<root/src/books/list.tt2> that we are using "| html", a
1479type of TT filter, to escape characters such as E<lt> and E<gt> to &lt;
1480and &gt; and avoid various types of dangerous hacks against your
1481application. In a real application, you would probably want to put "|
1482html" at the end of every field where a user has control over the
1483information that can appear in that field (and can therefore inject
1484markup or code if you don't "neutralize" those fields). In addition to
1485"| html", Template Toolkit has a variety of other useful filters that
c988c8b0 1486can found in the documentation for L<Template::Filters>. (While we are
1487on the topic of security and escaping of dangerous values, one of the
1488advantages of using tools like DBIC for database access or
1489L<HTML::FormFu> for form management [see
1490L<Chapter 9|Catalyst::Manual::Tutorial::09_AdvancedCRUD::09_FormFu>
1491is that they automatically handle most escaping for you and therefore
1492dramatically increase the security of your app.)
3533daff 1493
1494
1390ef0e 1495=head1 RUNNING THE APPLICATION FROM THE COMMAND LINE
1496
a8f4e284 1497In some situations, it can be useful to run your application and display
1498a page without using a browser. Catalyst lets you do this using the
1499C<scripts/myapp_test.pl> script. Just supply the URL you wish to
1500display and it will run that request through the normal controller
1501dispatch logic and use the appropriate view to render the output
1502(obviously, complex pages may dump a lot of text to your terminal
1503window). For example, if you type:
1390ef0e 1504
1505 $ script/myapp_test.pl "/books/list"
1506
1507You should get the same text as if you visited
1508L<http://localhost:3000/books/list> with the normal development server
c988c8b0 1509and asked your browser to view the page source. You can even pipe this
1510HTML text output to a text-based browser using a command like:
1511
1512 $ script/myapp_test.pl "/books/list" | lynx -stdin
1513
1514And you should see a fully rendered text-based view of your page. (If
1515you are following along in Debian 6, type
1516C<sudo aptitude -y install lynx> to install lynx.) If you do start
1517lynx, you can use the "Q" key to quit.
3533daff 1518
1390ef0e 1519
1520=head1 OPTIONAL INFORMATION
1521
4b4d3884 1522B<NOTE: The rest of this chapter of the tutorial is optional. You can
3ab6187c 1523skip to Chapter 4, L<Basic CRUD|Catalyst::Manual::Tutorial::04_BasicCRUD>,
3533daff 1524if you wish.>
1525
acbd7bdd 1526
8a472b34 1527=head2 Using 'RenderView' for the Default View
1390ef0e 1528
1529Once your controller logic has processed the request from a user, it
1530forwards processing to your view in order to generate the appropriate
3533daff 1531response output. Catalyst uses
2217b252 1532L<Catalyst::Action::RenderView> by default
a8f4e284 1533to automatically perform this operation. If you look in
1534C<lib/MyApp/Controller/Root.pm>, you should see the empty definition for
1535the C<sub end> method:
3533daff 1536
1537 sub end : ActionClass('RenderView') {}
1538
1390ef0e 1539The following bullet points provide a quick overview of the
3533daff 1540C<RenderView> process:
1541
1542=over 4
1543
1544=item *
1545
1546C<Root.pm> is designed to hold application-wide logic.
1547
1548=item *
1549
1390ef0e 1550At the end of a given user request, Catalyst will call the most specific
1551C<end> method that's appropriate. For example, if the controller for a
1552request has an C<end> method defined, it will be called. However, if
1553the controller does not define a controller-specific C<end> method, the
3533daff 1554"global" C<end> method in C<Root.pm> will be called.
1555
1556=item *
1557
1558Because the definition includes an C<ActionClass> attribute, the
c988c8b0 1559L<Catalyst::Action::RenderView> logic will be executed B<after> any code
1560inside the definition of C<sub end> is run. See
1561L<Catalyst::Manual::Actions> for more information on C<ActionClass>.
3533daff 1562
1563=item *
1564
1390ef0e 1565Because C<sub end> is empty, this effectively just runs the default
1566logic in C<RenderView>. However, you can easily extend the
1567C<RenderView> logic by adding your own code inside the empty method body
1568(C<{}>) created by the Catalyst Helpers when we first ran the
1569C<catalyst.pl> to initialize our application. See
2217b252 1570L<Catalyst::Action::RenderView> for more
4d63a0d5 1571detailed information on how to extend C<RenderView> in C<sub end>.
3533daff 1572
1573=back
1574
1575
fce83e5f 1576=head2 RenderView's "dump_info" Feature
1577
a8f4e284 1578One of the nice features of C<RenderView> is that it automatically
1579allows you to add C<dump_info=1> to the end of any URL for your
1580application and it will force the display of the "exception dump" screen
1581to the client browser. You can try this out by pointing your browser to
1582this URL:
fce83e5f 1583
1584 http://localhost:3000/books/list?dump_info=1
1585
1586You should get a page with the following message at the top:
1587
1588 Caught exception in MyApp::Controller::Root->end "Forced debug -
1589 Scrubbed output at /usr/share/perl5/Catalyst/Action/RenderView.pm line 46."
1590
a8f4e284 1591Along with a summary of your application's state at the end of the
1592processing for that request. The "Stash" section should show a
1593summarized version of the DBIC book model objects. If desired, you can
1594adjust the summarization logic (called "scrubbing" logic) -- see
2217b252 1595L<Catalyst::Action::RenderView> for
fce83e5f 1596details.
1597
a8f4e284 1598Note that you shouldn't need to worry about "normal clients" using this
1599technique to "reverse engineer" your application -- C<RenderView> only
1600supports the C<dump_info=1> feature when your application is running in
1601C<-Debug> mode (something you won't do once you have your application
1602deployed in production).
fce83e5f 1603
1604
3533daff 1605=head2 Using The Default Template Name
1606
1390ef0e 1607By default, C<Catalyst::View::TT> will look for a template that uses the
1608same name as your controller action, allowing you to save the step of
1609manually specifying the template name in each action. For example, this
c988c8b0 1610would allow us to remove the
1611C<$c-E<gt>stash-E<gt>{template} = 'books/list.tt2';>
1612line of our C<list> action in the Books controller.
a8f4e284 1613Open C<lib/MyApp/Controller/Books.pm> in your editor and comment out
1614this line to match the following (only the
1615C<$c-E<gt>stash-E<gt>{template}> line has changed):
3533daff 1616
1617 =head2 list
1618
1619 Fetch all book objects and pass to books/list.tt2 in stash to be displayed
1620
1621 =cut
1622
ddfbd850 1623 sub list :Local {
3533daff 1624 # Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
1625 # 'Context' that's used to 'glue together' the various components
1626 # that make up the application
1627 my ($self, $c) = @_;
1628
1629 # Retrieve all of the book records as book model objects and store in the
1630 # stash where they can be accessed by the TT template
0ed3df53 1631 $c->stash(books => [$c->model('DB::Book')->all]);
3533daff 1632
1633 # Set the TT template to use. You will almost always want to do this
1634 # in your action methods (actions methods respond to user input in
1635 # your controllers).
61cb69fd 1636 #$c->stash(template => 'books/list.tt2');
3533daff 1637 }
1638
3533daff 1639
6961c906 1640You should now be able to access the L<http://localhost:3000/books/list>
1641URL as before.
3533daff 1642
a8f4e284 1643B<NOTE:> Please note that if you use the default template technique, you
1644will B<not> be able to use either the C<$c-E<gt>forward> or the
1645C<$c-E<gt>detach> mechanisms (these are discussed in Chapter 2 and
4b4d3884 1646Chapter 9 of the Tutorial).
3533daff 1647
3b1fa91b 1648B<IMPORTANT:> Make sure that you do NOT skip the following section
1649before continuing to the next chapter 4 Basic CRUD.
3533daff 1650
fce83e5f 1651
4d63a0d5 1652=head2 Return To A Manually Specified Template
3533daff 1653
1654In order to be able to use C<$c-E<gt>forward> and C<$c-E<gt>detach>
a8f4e284 1655later in the tutorial, you should remove the comment from the statement
1656in C<sub list> in C<lib/MyApp/Controller/Books.pm>:
3533daff 1657
0ed3df53 1658 $c->stash(template => 'books/list.tt2');
3533daff 1659
a8f4e284 1660Then delete the C<TEMPLATE_EXTENSION> line in C<lib/MyApp/View/HTML.pm>.
3533daff 1661
a8f4e284 1662Check the L<http://localhost:3000/books/list> URL in your browser. It
1663should look the same manner as with earlier sections.
3533daff 1664
1665
1666=head1 AUTHOR
1667
1668Kennedy Clark, C<hkclark@gmail.com>
1669
53243324 1670Feel free to contact the author for any errors or suggestions, but the
1671best way to report issues is via the CPAN RT Bug system at
1672<https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Manual>.
1673
1674The most recent version of the Catalyst Tutorial can be found at
59884771 1675L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
3533daff 1676
ec3ef4ad 1677Copyright 2006-2010, Kennedy Clark, under the
1678Creative Commons Attribution Share-Alike License Version 3.0
8482d557 1679(L<http://creativecommons.org/licenses/by-sa/3.0/us/>).