Commit | Line | Data |
4d583dd8 |
1 | =head1 NAME |
2 | |
64ccd8a8 |
3 | Catalyst::Manual::Tutorial::CatalystBasics - Catalyst Tutorial - Part 2: Catalyst Application Development Basics |
4d583dd8 |
4 | |
5 | |
4d583dd8 |
6 | =head1 OVERVIEW |
7 | |
8 | This is B<Part 2 of 9> for the Catalyst tutorial. |
9 | |
64ccd8a8 |
10 | L<Tutorial Overview|Catalyst::Manual::Tutorial> |
4d583dd8 |
11 | |
12 | =over 4 |
13 | |
14 | =item 1 |
15 | |
16 | L<Introduction|Catalyst::Manual::Tutorial::Intro> |
17 | |
18 | =item 2 |
19 | |
20 | B<Catalyst Basics> |
21 | |
22 | =item 3 |
23 | |
653f4595 |
24 | L<Basic CRUD|Catalyst::Manual::Tutorial::BasicCRUD> |
4d583dd8 |
25 | |
26 | =item 4 |
27 | |
28 | L<Authentication|Catalyst::Manual::Tutorial::Authentication> |
29 | |
30 | =item 5 |
31 | |
32 | L<Authorization|Catalyst::Manual::Tutorial::Authorization> |
33 | |
34 | =item 6 |
35 | |
36 | L<Debugging|Catalyst::Manual::Tutorial::Debugging> |
37 | |
38 | =item 7 |
39 | |
40 | L<Testing|Catalyst::Manual::Tutorial::Testing> |
41 | |
42 | =item 8 |
43 | |
653f4595 |
44 | L<Advanced CRUD|Catalyst::Manual::Tutorial::AdvancedCRUD> |
4d583dd8 |
45 | |
46 | =item 9 |
47 | |
653f4595 |
48 | L<Appendices|Catalyst::Manual::Tutorial::Appendicies> |
4d583dd8 |
49 | |
50 | =back |
51 | |
4d583dd8 |
52 | =head1 DESCRIPTION |
53 | |
64ccd8a8 |
54 | In this part of the tutorial, we will create a very basic Catalyst web |
55 | application. Though simple in many respects, this section will already |
56 | demonstrate a number of powerful capabilities such as: |
4d583dd8 |
57 | |
58 | =over 4 |
59 | |
60 | =item * Helper Scripts |
61 | |
64ccd8a8 |
62 | Catalyst helper scripts that can be used to rapidly bootstrap the |
63 | skeletal structure of an application. |
4d583dd8 |
64 | |
65 | =item * MVC |
66 | |
64ccd8a8 |
67 | Model/View/Controller (MVC) provides an architecture that facilitates a |
68 | clean "separation of control" between the different portions of your |
653f4595 |
69 | application. Given that many other documents cover this subject in |
64ccd8a8 |
70 | detail, MVC will not be discussed in depth here (for an excellent |
71 | introduction to MVC and general Catalyst concepts, please see |
653f4595 |
72 | L<Catalyst::Manual::About>. In short: |
4d583dd8 |
73 | |
74 | =over 4 |
75 | |
76 | =item * Model |
77 | |
653f4595 |
78 | The model usually represents a data store. In most applications, the |
79 | model equates to the objects that are created from and saved to your SQL |
80 | database. |
4d583dd8 |
81 | |
82 | =item * View |
83 | |
64ccd8a8 |
84 | The view takes model objects and renders them into something for the end |
653f4595 |
85 | user to look at. Normally this involves a template-generation tool that |
64ccd8a8 |
86 | creates HTML for the user's web browser, but it could easily be code |
653f4595 |
87 | that generates other forms such as PDF documents, e-mails, or Excel |
88 | spreadsheets. |
4d583dd8 |
89 | |
90 | =item * Controller |
91 | |
64ccd8a8 |
92 | As suggested by its name, the controller takes user requests and routes |
93 | them to the necessary model and view. |
4d583dd8 |
94 | |
95 | =back |
96 | |
97 | =item * ORM |
98 | |
653f4595 |
99 | The use of Object-Relational Mapping (ORM) technology for database |
100 | access. Specifically, ORM provides an automated and standardized means |
101 | to persist and restore objects to/from a relational database. |
4d583dd8 |
102 | |
103 | =back |
104 | |
64ccd8a8 |
105 | B<TIP>: Note that all of the code for this part of the tutorial can be |
106 | pulled from the Catalyst Subversion repository in one step with the |
107 | following command: |
4d583dd8 |
108 | |
109 | svn checkout http://dev.catalyst.perl.org/repos/Catalyst/trunk/examples/Tutorial@### |
110 | IMPORTANT: Does not work yet. Will be completed for final version. |
111 | |
112 | |
4d583dd8 |
113 | =head1 CREATE A CATALYST PROJECT |
114 | |
64ccd8a8 |
115 | Catalyst provides a number of helper scripts that can be used to quickly |
653f4595 |
116 | flesh out the basic structure of your application. All Catalyst projects |
117 | begin with the C<catalyst.pl> helper. |
4d583dd8 |
118 | |
64ccd8a8 |
119 | In the case of this tutorial, use the Catalyst C<catalyst.pl> script to |
120 | initialize the framework for an application called C<MyApp>: |
4d583dd8 |
121 | |
122 | $ catalyst.pl MyApp |
123 | $ cd MyApp |
124 | |
64ccd8a8 |
125 | The C<catalyst.pl> helper script will display the names of the |
126 | directories and files it creates. |
4d583dd8 |
127 | |
653f4595 |
128 | Though it's too early for any significant celebration, we already have a |
129 | functioning application. Run the following command to run this |
130 | application with the built-in development web server: |
4d583dd8 |
131 | |
132 | $ script/myapp_server.pl |
133 | |
64ccd8a8 |
134 | Point your web browser to L<http://localhost:3000> (substituting a |
135 | different hostname or IP address as appropriate) and you should be |
136 | greeted by the Catalyst welcome screen. Press Ctrl-C to break out of |
137 | the development server. |
4d583dd8 |
138 | |
4d583dd8 |
139 | =head1 CREATE A SQLITE DATABASE |
140 | |
64ccd8a8 |
141 | In this step, we make a text file with the required SQL commands to |
142 | create a database table and load some sample data. Open C<myapp01.sql> |
143 | in your editor and enter: |
4d583dd8 |
144 | |
145 | -- |
146 | -- Create a very simple database to hold book and author information |
147 | -- |
148 | CREATE TABLE books ( |
149 | id INTEGER PRIMARY KEY, |
150 | title TEXT , |
151 | rating INTEGER |
152 | ); |
153 | -- 'book_authors' is a many-to-many join table between books & authors |
154 | CREATE TABLE book_authors ( |
155 | book_id INTEGER, |
156 | author_id INTEGER, |
157 | PRIMARY KEY (book_id, author_id) |
158 | ); |
159 | CREATE TABLE authors ( |
160 | id INTEGER PRIMARY KEY, |
161 | first_name TEXT, |
162 | last_name TEXT |
163 | ); |
164 | --- |
165 | --- Load some sample data |
166 | --- |
167 | INSERT INTO books VALUES (1, 'CCSP SNRS Exam Certification Guide', 5); |
168 | INSERT INTO books VALUES (2, 'TCP/IP Illustrated, Volume 1', 5); |
169 | INSERT INTO books VALUES (3, 'Internetworking with TCP/IP Vol.1', 4); |
170 | INSERT INTO books VALUES (4, 'Perl Cookbook', 5); |
171 | INSERT INTO books VALUES (5, 'Designing with Web Standards', 5); |
172 | INSERT INTO authors VALUES (1, 'Greg', 'Bastien'); |
173 | INSERT INTO authors VALUES (2, 'Sara', 'Nasseh'); |
174 | INSERT INTO authors VALUES (3, 'Christian', 'Degu'); |
175 | INSERT INTO authors VALUES (4, 'Richard', 'Stevens'); |
176 | INSERT INTO authors VALUES (5, 'Douglas', 'Comer'); |
177 | INSERT INTO authors VALUES (6, 'Tom', 'Christiansen'); |
178 | INSERT INTO authors VALUES (7, ' Nathan', 'Torkington'); |
179 | INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman'); |
180 | INSERT INTO book_authors VALUES (1, 1); |
181 | INSERT INTO book_authors VALUES (1, 2); |
182 | INSERT INTO book_authors VALUES (1, 3); |
183 | INSERT INTO book_authors VALUES (2, 4); |
184 | INSERT INTO book_authors VALUES (3, 5); |
185 | INSERT INTO book_authors VALUES (4, 6); |
186 | INSERT INTO book_authors VALUES (4, 7); |
187 | INSERT INTO book_authors VALUES (5, 8); |
188 | |
64ccd8a8 |
189 | B<TIP>: See Appendix 1 for tips on removing the leading spaces when |
653f4595 |
190 | cutting and pasting example code from POD documents. |
4d583dd8 |
191 | |
192 | Then use the following command to build a C<myapp.db> SQLite database: |
193 | |
194 | $ sqlite3 myapp.db < myapp01.sql |
195 | |
64ccd8a8 |
196 | If you need to create the database more than once, you probably want to |
197 | issue the C<rm myapp.db> command to delete the database before you use |
198 | the C<sqlite3 myapp.db < myapp01.sql> command. |
4d583dd8 |
199 | |
64ccd8a8 |
200 | Once the C<myapp.db> database file has been created and initialized, you |
201 | can use the SQLite command line environment to do a quick dump of the |
202 | database contents: |
4d583dd8 |
203 | |
204 | $ sqlite3 myapp.db |
205 | SQLite version 3.2.2 |
206 | Enter ".help" for instructions |
207 | sqlite> select * from books; |
208 | 1|CCSP SNRS Exam Certification Guide|5 |
209 | 2|TCP/IP Illustrated, Volume 1|5 |
210 | 3|Internetworking with TCP/IP Vol.1|4 |
211 | 4|Perl Cookbook|5 |
212 | 5|Designing with Web Standards|5 |
213 | sqlite> .q |
214 | $ |
215 | |
216 | Or: |
217 | |
218 | $ sqlite3 myapp.db "select * from books" |
219 | 1|CCSP SNRS Exam Certification Guide|5 |
220 | 2|TCP/IP Illustrated, Volume 1|5 |
221 | 3|Internetworking with TCP/IP Vol.1|4 |
222 | 4|Perl Cookbook|5 |
223 | 5|Designing with Web Standards|5 |
224 | |
64ccd8a8 |
225 | As with most other SQL tools, if you are using the full "interactive" |
226 | environment you need to terminate your SQL commands with a ";" (it's not |
227 | required if you do a single SQL statement on the command line). Use |
228 | ".q" to exit from SQLite from the SQLite interactive mode and return to |
229 | your OS command prompt. |
4d583dd8 |
230 | |
231 | |
4d583dd8 |
232 | =head1 EDIT THE LIST OF CATALYST PLUGINS |
233 | |
64ccd8a8 |
234 | One of the greatest benefits of Catalyst is that it has such a large |
235 | library of plugins available. Plugins are used to seamlessly integrate |
236 | existing Perl modules into the overall Catalyst framework. In general, |
237 | they do this by adding additional methods to the C<context> object |
238 | (generally written as C<$c>) that Catalyst passes to every component |
239 | throughout the framework. |
4d583dd8 |
240 | |
241 | By default, Catalyst enables three plugins/flags: |
242 | |
243 | =over 4 |
244 | |
245 | =item * |
246 | |
247 | C<-Debug> Flag |
248 | |
64ccd8a8 |
249 | Enables the Catalyst debug output you saw when we started the |
250 | C<script/myapp_server.pl> development server earlier. You can remove |
251 | this plugin when you place your application into production. |
4d583dd8 |
252 | |
64ccd8a8 |
253 | As you may have noticed, C<-Debug> is not a plugin, but a I<flag>. |
254 | Although most of the items specified on the C<use Catalyst> line of your |
255 | application class will be plugins, Catalyst supports a limited number of |
14e6feb0 |
256 | flag options (of these, C<-Debug> is the most common). See the |
257 | documentation for C<Catalyst.pm> to get details on other flags |
258 | (currently C<-Engine>, C<-Home>, and C<-Log>). |
4d583dd8 |
259 | |
64ccd8a8 |
260 | If you prefer, you can use the C<$c-E<gt>debug> method to enable debug |
261 | messages. |
4d583dd8 |
262 | |
263 | =item * |
264 | |
265 | L<Catalyst::Plugin::ConfigLoader|Catalyst::Plugin::ConfigLoader> |
266 | |
653f4595 |
267 | C<ConfigLoader> provides an automatic way to load configurable |
64ccd8a8 |
268 | parameters for your application from a central YAML file (versus having |
269 | the values hard-coded inside your Perl modules). If you have not been |
270 | exposed to YAML before, it is a human-readable data serialization format |
271 | that can be used to read (and write) values to/from text files. We will |
272 | see how to use this feature of Catalyst during the authentication and |
273 | authorization sections (Part 4 and Part 5). |
4d583dd8 |
274 | |
4d583dd8 |
275 | =item * |
276 | |
14e6feb0 |
277 | L<Catalyst::Plugin::Static::Simple|Catalyst::Plugin::Static::Simple> |
4d583dd8 |
278 | |
64ccd8a8 |
279 | C<Static::Simple> provides an easy method of serving static content such |
280 | as images and CSS files under the development server. |
4d583dd8 |
281 | |
282 | =back |
283 | |
64ccd8a8 |
284 | To modify the list of plugins, edit C<lib/MyApp.pm> (this file is |
285 | generally referred to as your I<application class>) and delete the line |
286 | with: |
4d583dd8 |
287 | |
288 | use Catalyst qw/-Debug ConfigLoader Static::Simple/; |
289 | |
290 | Replace it with: |
291 | |
292 | use Catalyst qw/ |
293 | -Debug |
294 | ConfigLoader |
295 | Static::Simple |
296 | |
4d583dd8 |
297 | StackTrace |
298 | DefaultEnd |
299 | /; |
300 | |
14e6feb0 |
301 | This tells Catalyst to start using two new plugins: |
4d583dd8 |
302 | |
303 | =over 4 |
304 | |
305 | =item * |
306 | |
4d583dd8 |
307 | L<Catalyst::Plugin::StackTrace|Catalyst::Plugin::StackTrace> |
308 | |
64ccd8a8 |
309 | Adds a stack trace to the standard Catalyst "debug screen" (this is the |
310 | screen Catalyst sends to your browser when an error occurs). |
4d583dd8 |
311 | |
c19d127e |
312 | Note: L<StackTrace|Catalyst::Plugin::StackTrace> output appears in your |
313 | browser, not in the console window from which you're running your |
314 | application, which is where logging output usually goes. |
4d583dd8 |
315 | |
316 | =item * |
317 | |
318 | L<Catalyst::Plugin::DefaultEnd|Catalyst::Plugin::DefaultEnd> |
319 | |
64ccd8a8 |
320 | Automatically provides a Catalyst "end action" that invokes your view at |
321 | the end of each request. Also allows you to add "dump_info=1" (precede |
322 | with "?" or "&" depending on where it is in the URL) to I<force> the |
323 | debug screen at the end of the Catalyst request processing cycle. |
4d583dd8 |
324 | |
653f4595 |
325 | B<TIP>: Many Catalyst-related documents predate |
64ccd8a8 |
326 | L<DefaultEnd|Catalyst::Plugin::DefaultEnd> and suggest that you add an |
327 | C<end> action to your application class (C<MyApp.pm>) or Root.pm |
328 | (C<MyApp/Controller/Root.pm>). In most of these cases, you can convert |
329 | to L<DefaultEnd|Catalyst::Plugin::DefaultEnd> by deleting the C<end> |
71dedf57 |
330 | action and using the plugin instead. There are certainly cases when |
331 | you'd want to write your own custom C<end> action, but for most |
332 | circumstances, DefaultEnd will be exactly what you want. |
4d583dd8 |
333 | |
334 | =back |
335 | |
64ccd8a8 |
336 | Note that when specifying plugins on the C<use Catalyst> line, you can |
71dedf57 |
337 | omit C<Catalyst::Plugin::> from the name. Additionally, you can spread |
64ccd8a8 |
338 | the plugin names across multiple lines as shown here, or place them all |
339 | on one (or more) lines as with the default configuration. |
4d583dd8 |
340 | |
4d583dd8 |
341 | =head1 DATABASE ACCESS WITH C<DBIx::Class> |
342 | |
64ccd8a8 |
343 | Catalyst can be used with virtually any form of persistent datastore |
14e6feb0 |
344 | available via Perl. For example, |
345 | L<Catalyst::Model::DBI|Catalyst::Model::DBI> can be used to |
346 | easily access databases through the traditional Perl C<DBI> interface. |
71dedf57 |
347 | However, most Catalyst applications use some form of ORM technology to |
64ccd8a8 |
348 | automatically create and save model objects as they are used. Although |
14e6feb0 |
349 | Tony Bowden's L<Class::DBI|Class::DBI> has been the traditional |
350 | Perl ORM engine, Matt Trout's L<DBIx::Class|DBIx::Class> (abbreviated |
351 | as "DBIC") has rapidly emerged as the Perl-based ORM technology of choice. |
352 | Most new Catalyst applications rely on DBIC, as will this tutorial. |
4d583dd8 |
353 | |
14e6feb0 |
354 | Note: See L<Catalyst:: Model::CDBI> for more information on using |
355 | Catalyst with L<Class::DBI|Class::DBI>. |
4d583dd8 |
356 | |
357 | =head2 Create a DBIC Schema File |
358 | |
64ccd8a8 |
359 | DBIx::Class uses a schema file to load other classes that represent the |
360 | tables in your database (DBIC refers to these "table objects" as "result |
71dedf57 |
361 | sources"; see L<DBIx::Class::ResultSource>). In this case, we want to |
362 | load the model object for the C<books>, C<book_authors>, and C<authors> |
363 | tables created in the previous step. |
4d583dd8 |
364 | |
365 | Open C<lib/MyAppDB.pm> in your editor and insert: |
366 | |
367 | package MyAppDB; |
368 | |
369 | =head1 NAME |
370 | |
71dedf57 |
371 | MyAppDB - DBIC Schema Class |
4d583dd8 |
372 | |
373 | =cut |
374 | |
375 | # Our schema needs to inherit from 'DBIx::Class::Schema' |
376 | use base qw/DBIx::Class::Schema/; |
377 | |
378 | # Need to load the DB Model classes here. |
379 | # You can use this syntax if you want: |
380 | # __PACKAGE__->load_classes(qw/Book BookAuthor Author/); |
381 | # Also, if you simply want to load all of the classes in a directory |
382 | # of the same name as your schema class (as we do here) you can use: |
383 | # __PACKAGE__->load_classes(qw//); |
384 | # But the variation below is more flexible in that it can be used to |
385 | # load from multiple namespaces. |
386 | __PACKAGE__->load_classes({ |
387 | MyAppDB => [qw/Book BookAuthor Author/] |
388 | }); |
389 | |
390 | 1; |
391 | |
64ccd8a8 |
392 | B<Note:> C<__PACKAGE__> is just a shorthand way of referencing the name |
393 | of the package where it is used. Therefore, in C<MyAppDB.pm>, |
14e6feb0 |
394 | C<__PACKAGE__> is equivalent to C<MyAppDB>. |
4d583dd8 |
395 | |
396 | |
397 | =head2 Create the DBIC "Result Source" Files |
398 | |
64ccd8a8 |
399 | In this step, we create "table classes" (again, these are called a |
71dedf57 |
400 | "result source" classes in DBIC) that act as model objects for the |
64ccd8a8 |
401 | C<books>, C<book_authors>, and C<authors> tables in our database. |
4d583dd8 |
402 | |
403 | First, create a directory to hold the class: |
404 | |
405 | $ mkdir lib/MyAppDB |
406 | |
407 | Then open C<lib/MyAppDB/Book.pm> in your editor and enter: |
408 | |
409 | package MyAppDB::Book; |
410 | |
411 | use base qw/DBIx::Class/; |
412 | |
413 | # Load required DBIC stuff |
414 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
415 | # Set the table name |
416 | __PACKAGE__->table('books'); |
417 | # Set columns in table |
418 | __PACKAGE__->add_columns(qw/id title rating/); |
419 | # Set the primary key for the table |
420 | __PACKAGE__->set_primary_key(qw/id/); |
421 | |
422 | # |
423 | # Set relationships: |
424 | # |
425 | |
426 | # has_many(): |
427 | # args: |
428 | # 1) Name of relationship, DBIC will create accessor with this name |
429 | # 2) Name of the model class referenced by this relationship |
430 | # 3) Column name in *foreign* table |
431 | __PACKAGE__->has_many(book_authors => 'MyAppDB::BookAuthor', 'book_id'); |
432 | |
433 | # many_to_many(): |
434 | # args: |
435 | # 1) Name of relationship, DBIC will create accessor with this name |
436 | # 2) Name of has_many() relationship this many_to_many() is shortcut for |
437 | # 3) Name of belongs_to() relationship in model class of has_many() above |
438 | # You must already have the has_many() defined to use a many_to_many(). |
439 | __PACKAGE__->many_to_many(authors => 'book_authors', 'author'); |
440 | |
441 | |
442 | =head1 NAME |
443 | |
444 | MyAppDB::Book - A model object representing a book. |
445 | |
446 | =head1 DESCRIPTION |
447 | |
448 | This is an object that represents a row in the 'books' table of your application |
449 | database. It uses DBIx::Class (aka, DBIC) to do ORM. |
450 | |
451 | For Catalyst, this is designed to be used through MyApp::Model::MyAppDB. |
452 | Offline utilities may wish to use this class directly. |
453 | |
454 | =cut |
455 | |
456 | 1; |
457 | |
64ccd8a8 |
458 | This defines both a C<has_many> and a C<many_to_many> relationship. The |
459 | C<many_to_many> relationship is optional, but it makes it easier to map |
460 | a book to its collection of authors. Without it, we would have to |
461 | "walk" though the C<book_authors> table as in |
462 | C<$book-E<gt>book_authors-E<gt>first-E<gt>author-E<gt>last_name> (we |
463 | will see examples on how to use DBIC objects in your code soon, but note |
464 | that because C<$book-E<gt>book_authors> can return multiple authors, we |
71dedf57 |
465 | have to use C<first> to display a single author). C<many_to_many> allows |
466 | us to use the shorter C<$book-E<gt>authors-E<gt>first-E<gt>last_name>. |
467 | Note that you cannot define a C<many_to_many> relationship without also |
468 | having the C<has_many> relationship in place. |
4d583dd8 |
469 | |
470 | Next, open C<lib/MyAppDB/Author.pm> in your editor and enter: |
471 | |
472 | package MyAppDB::Author; |
473 | |
474 | use base qw/DBIx::Class/; |
475 | |
476 | # Load required DBIC stuff |
477 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
478 | # Set the table name |
479 | __PACKAGE__->table('authors'); |
480 | # Set columns in table |
481 | __PACKAGE__->add_columns(qw/id first_name last_name/); |
482 | # Set the primary key for the table |
483 | __PACKAGE__->set_primary_key(qw/id/); |
484 | |
485 | # |
486 | # Set relationships: |
487 | # |
488 | |
489 | # has_many(): |
490 | # args: |
491 | # 1) Name of relationship, DBIC will create accessor with this name |
492 | # 2) Name of the model class referenced by this relationship |
493 | # 3) Column name in *foreign* table |
494 | __PACKAGE__->has_many(book_author => 'MyAppDB::BookAuthor', 'author_id'); |
495 | |
496 | # many_to_many(): |
497 | # args: |
498 | # 1) Name of relationship, DBIC will create accessor with this name |
499 | # 2) Name of has_many() relationship this many_to_many() is shortcut for |
500 | # 3) Name of belongs_to() relationship in model class of has_many() above |
501 | # You must already have the has_many() defined to use a many_to_many(). |
502 | __PACKAGE__->many_to_many(books => 'book_author', 'book'); |
503 | |
504 | |
505 | =head1 NAME |
506 | |
507 | MyAppDB::Author - A model object representing an author of a book (if a book has |
508 | multiple authors, each will be represented be separate Author object). |
509 | |
510 | =head1 DESCRIPTION |
511 | |
512 | This is an object that represents a row in the 'authors' table of your application |
513 | database. It uses DBIx::Class (aka, DBIC) to do ORM. |
514 | |
515 | For Catalyst, this is designed to be used through MyApp::Model::MyAppDB. |
516 | Offline utilities may wish to use this class directly. |
517 | |
518 | =cut |
519 | |
520 | 1; |
521 | |
522 | Finally, open C<lib/MyAppDB/BookAuthor.pm> in your editor and enter: |
523 | |
524 | package MyAppDB::BookAuthor; |
525 | |
526 | use base qw/DBIx::Class/; |
527 | |
528 | # Load required DBIC stuff |
529 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
530 | # Set the table name |
531 | __PACKAGE__->table('book_authors'); |
532 | # Set columns in table |
533 | __PACKAGE__->add_columns(qw/book_id author_id/); |
534 | # Set the primary key for the table |
535 | __PACKAGE__->set_primary_key(qw/book_id author_id/); |
536 | |
537 | # |
538 | # Set relationships: |
539 | # |
540 | |
541 | # belongs_to(): |
542 | # args: |
543 | # 1) Name of relationship, DBIC will create accessor with this name |
544 | # 2) Name of the model class referenced by this relationship |
545 | # 3) Column name in *this* table |
546 | __PACKAGE__->belongs_to(book => 'MyAppDB::Book', 'book_id'); |
547 | |
548 | # belongs_to(): |
549 | # args: |
550 | # 1) Name of relationship, DBIC will create accessor with this name |
551 | # 2) Name of the model class referenced by this relationship |
552 | # 3) Column name in *this* table |
553 | __PACKAGE__->belongs_to(author => 'MyAppDB::Author', 'author_id'); |
554 | |
555 | |
556 | =head1 NAME |
557 | |
558 | MyAppDB::BookAuthor - A model object representing the JOIN between an author and |
559 | a book. |
560 | |
561 | =head1 DESCRIPTION |
562 | |
563 | This is an object that represents a row in the 'book_authors' table of your |
564 | application database. It uses DBIx::Class (aka, DBIC) to do ORM. |
565 | |
566 | You probably won't need to use this class directly -- it will be automatically |
567 | used by DBIC where joins are needed. |
568 | |
569 | For Catalyst, this is designed to be used through MyApp::Model::MyAppDB. |
570 | Offline utilities may wish to use this class directly. |
571 | |
572 | =cut |
573 | |
574 | 1; |
575 | |
64ccd8a8 |
576 | B<Note:> This sample application uses a plural form for the database |
577 | tables (e.g., C<books> and C<authors>) and a singular form for the model |
578 | objects (e.g., C<Book> and C<Author>); however, Catalyst places no |
579 | restrictions on the naming conventions you wish to use. |
4d583dd8 |
580 | |
4d583dd8 |
581 | =head2 Use C<Catalyst::Model::DBIC::Schema> To Load The Model Class |
582 | |
14e6feb0 |
583 | When L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema> is |
64ccd8a8 |
584 | in use, Catalyst essentially reads an existing copy of your database |
585 | model and creates a new set of objects under C<MyApp::Model> for use |
586 | inside of Catalyst. |
4d583dd8 |
587 | |
14e6feb0 |
588 | B<Note:> With |
589 | L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema> you |
590 | essentially end up with two sets of model classes (only one of which |
591 | you write... the other set is created automatically in memory when |
592 | your Catalyst application initializes). For this tutorial application, |
593 | the important points to remember are: you write the I<result source> |
594 | files in C<MyAppDB>, but I<within Catalyst> you use the I<automatically |
595 | created model classes> in C<MyApp::Model>. |
4d583dd8 |
596 | |
14e6feb0 |
597 | Use the |
598 | L<Catalyst::Helper::Model::DBIC::Schema|Catalyst::Helper::Model::DBIC::Schema> |
599 | helper script to create the model class that loads up the model we |
600 | created in the previous step: |
4d583dd8 |
601 | |
602 | $ script/myapp_create.pl model MyAppDB DBIC::Schema MyAppDB dbi:SQLite:myapp.db '' '' '{ AutoCommit => 1 }' |
603 | |
64ccd8a8 |
604 | Where the first C<MyAppDB> is the name of the class to be created by the |
605 | helper in C<lib/MyApp/Model> and the second C<MyAppDB> is the name of |
606 | existing schema file we created (in C<lib/MyAppDB.pm>). You can see |
607 | that the helper creates a model file under C<lib/MyApp/Model> (Catalyst |
608 | has a separate directory under C<lib/MyApp> for each of the three parts |
609 | of MVC: C<Model>, C<View>, and C<Controller> [although older Catalyst |
610 | applications often use the directories C<M>, C<V>, and C<C>]). |
4d583dd8 |
611 | |
612 | |
4d583dd8 |
613 | =head1 CREATE A CATALYST CONTROLLER |
614 | |
71dedf57 |
615 | Controllers are where you write methods that interact with user |
616 | input--typically, controller methods respond to C<GET> and C<POST> |
617 | messages from the user's web browser. |
4d583dd8 |
618 | |
71dedf57 |
619 | Use the Catalyst C<create> script to add a controller for book-related |
620 | actions: |
4d583dd8 |
621 | |
622 | $ script/myapp_create.pl controller Books |
623 | |
64ccd8a8 |
624 | Then edit C<lib/MyApp/Controller/Books.pm> and add the following method |
625 | to the controller: |
4d583dd8 |
626 | |
627 | =head2 list |
628 | |
629 | Fetch all book objects and pass to books/list.tt2 in stash to be displayed |
630 | |
631 | =cut |
632 | |
633 | sub list : Local { |
634 | # Retrieve the usual perl OO '$self' for this object. $c is the Catalyst |
635 | # 'Context' that's used to 'glue together' the various components |
636 | # that make up the application |
637 | my ($self, $c) = @_; |
638 | |
639 | # Retrieve all of the book records as book model objects and store in the |
640 | # stash where they can be accessed by the TT template |
641 | $c->stash->{books} = [$c->model('MyAppDB::Book')->all]; |
642 | |
643 | # Set the TT template to use. You will almost always want to do this |
644 | # in your action methods. |
645 | $c->stash->{template} = 'books/list.tt2'; |
646 | } |
647 | |
64ccd8a8 |
648 | B<Note:> Programmers experienced with object-oriented Perl should |
649 | recognize C<$self> as a reference to the object where this method was |
650 | called. On the other hand, C<$c> will be new to many Perl programmers |
651 | who have not used Catalyst before (it's sometimes written as |
652 | C<$context>). The Context object is automatically passed to all |
653 | Catalyst components. It is used to pass information between components |
654 | and provide access to Catalyst and plugin functionality. |
4d583dd8 |
655 | |
64ccd8a8 |
656 | B<TIP>: You may see the C<$c-E<gt>model('MyAppDB::Book')> used above |
657 | written as C<$c-E<gt>model('MyAppDB')-E<gt>resultset('Book)>. The two |
658 | are equivalent. |
4d583dd8 |
659 | |
64ccd8a8 |
660 | B<Note:> Catalyst actions are regular Perl methods, but they make use of |
14e6feb0 |
661 | Nicholas Clark's C<attributes> module (that's the C<: Local> next to the |
662 | C<sub list> in the code above) to provide additional information to the |
663 | Catalyst dispatcher logic. |
4d583dd8 |
664 | |
4d583dd8 |
665 | =head1 CATALYST VIEWS |
666 | |
71dedf57 |
667 | Views are where you render output, typically for display in the user's |
14e6feb0 |
668 | web browser, but also possibly using other display output-generation |
71dedf57 |
669 | systems. As with virtually every aspect of Catalyst, options abound |
670 | when it comes to the specific view technology you adopt inside your |
671 | application. However, most Catalyst applications use the Template |
672 | Toolkit, known as TT (for more information on TT, see |
673 | L<http://www.template-toolkit.org>). Other popular View technologies |
674 | include Mason (L<http://www.masonhq.com> and |
14e6feb0 |
675 | L<http://www.masonbook.com>) and L<HTML::Template|HTML::Template> |
64ccd8a8 |
676 | (L<http://html-template.sourceforge.net>). |
4d583dd8 |
677 | |
4d583dd8 |
678 | =head2 Create a Catalyst View Using C<TTSITE> |
679 | |
680 | When using TT for the Catalyst view, there are two main helper scripts: |
681 | |
682 | =over 4 |
683 | |
684 | =item * |
685 | |
14e6feb0 |
686 | L<Catalyst::Helper::View::TT|Catalyst::Helper::View::TT> |
4d583dd8 |
687 | |
688 | =item * |
689 | |
14e6feb0 |
690 | L<Catalyst::Helper::View::TTSite|Catalyst::Helper::View::TTSite> |
4d583dd8 |
691 | |
692 | =back |
693 | |
64ccd8a8 |
694 | Both are similar, but C<TT> merely creates the C<lib/MyApp/View/TT.pm> |
695 | file and leaves the creation of any hierarchical template organization |
71dedf57 |
696 | entirely up to you. (It also creates a C<t/view_TT.t> file for testing; |
697 | test cases will be discussed in Part 7). The C<TTSite> helper creates a |
698 | modular and hierarchical view layout with separate Template Toolkit (TT) |
699 | files for common header and footer information, configuration values, a |
700 | CSS stylesheet, and more. |
4d583dd8 |
701 | |
64ccd8a8 |
702 | Enter the following command to enable the C<TTSite> style of view |
71dedf57 |
703 | rendering for this tutorial: |
4d583dd8 |
704 | |
705 | $ script/myapp_create.pl view TT TTSite |
706 | |
64ccd8a8 |
707 | This puts a number of files in the C<root/lib> and C<root/src> |
708 | directories that can be used to customize the look and feel of your |
709 | application. Also take a look at C<lib/MyApp/View/TT.pm> for config |
710 | values set by the C<TTSite> helper. |
711 | |
712 | B<TIP>: Note that TTSite does one thing that could confuse people who |
713 | are used to the normal C<TT> Catalyst View: it redefines the Catalyst |
71dedf57 |
714 | context object in templates from its usual C<c> to C<Catalyst>. When |
715 | looking at other Catalyst examples, remember that they almost always use |
716 | C<c>. Note that Catalyst and TT I<do not complain> when you use the |
717 | wrong name to access the context object...TT simply outputs blanks for |
5c1f2a06 |
718 | that bogus logic (see next tip to change this behavior with TT C<DEBUG> |
719 | options). Finally, be aware that this change in name I<only> |
71dedf57 |
720 | applies to how the context object is accessed inside your TT templates; |
64ccd8a8 |
721 | your controllers will continue to use C<$c> (or whatever name you use |
71dedf57 |
722 | when fetching the reference from C<@_> inside your methods). (You can |
64ccd8a8 |
723 | change back to the "default" behavior be removing the C<CATALYST_VAR> |
724 | line from C<lib/MyApp/View/TT.pm>, but you will also have to edit |
725 | C<root/lib/config/main> and C<root/lib/config/url>. If you do this, be |
726 | careful not to have a collision between your own C<c> variable and the |
727 | Catalyst C<c> variable.) |
4d583dd8 |
728 | |
5c1f2a06 |
729 | B<TIP>: When troubleshooting TT it can be helpful to enable variable |
730 | C<DEBUG> options. You can do this in a Catalyst environment by adding |
731 | a C<DEBUG> line to the C<__PACKAGE__->config> declaration in |
732 | C<MyApp/View/TT.pm>: |
733 | |
734 | __PACKAGE__->config({ |
735 | CATALYST_VAR => 'Catalyst', |
736 | ... |
737 | DEBUG => 'undef', |
738 | ... |
739 | }); |
740 | |
741 | There are a variety of options you can use, such as 'undef', 'all', |
742 | 'service', 'context', 'parser', 'provider', and 'service'. See |
743 | L<Template::Constants> for more information (remove the C<DEBUG_> |
744 | portion of the name shown in the TT docs and convert to lower case |
745 | for use inside Catalyst). |
746 | |
747 | |
4d583dd8 |
748 | =head2 Globally Customize Every View |
749 | |
64ccd8a8 |
750 | When using TTSite, files in the subdirectories of C<root/lib> can be |
751 | used to make changes that will appear in every view. For example, to |
752 | display optional status and error messages in every view, edit |
71dedf57 |
753 | C<root/lib/site/layout>, updating it to match the following (the two HTML |
64ccd8a8 |
754 | C<span> elements are new): |
4d583dd8 |
755 | |
756 | <div id="header">[% PROCESS site/header %]</div> |
757 | |
758 | <div id="content"> |
759 | <span class="message">[% status_msg %]</span> |
760 | <span class="error">[% error_msg %]</span> |
761 | [% content %] |
762 | </div> |
763 | |
764 | <div id="footer">[% PROCESS site/footer %]</div> |
765 | |
64ccd8a8 |
766 | If we set either message in the Catalyst stash (e.g., |
71dedf57 |
767 | C<$c-E<gt>stash-E<gt>{status_msg} = 'Request was successful!'>) it will |
768 | be displayed whenever any view used by that request is rendered. The |
769 | C<message> and C<error> CSS styles are automatically defined in |
770 | C<root/src/ttsite.css> and can be customized to suit your needs. |
4d583dd8 |
771 | |
64ccd8a8 |
772 | B<Note:> The Catalyst stash only lasts for a single HTTP request. If |
773 | you need to retain information across requests you can use |
14e6feb0 |
774 | L<Catalyst::Plugin::Session|Catalyst::Plugin::Session> (we will use |
775 | Catalyst sessions in the Authentication part of the tutorial). |
4d583dd8 |
776 | |
777 | |
778 | =head2 Create a TT Template Page |
779 | |
64ccd8a8 |
780 | To add a new page of content to the TTSite view hierarchy, just create a |
781 | new C<.tt2> file in C<root/src>. Only include HTML markup that goes |
782 | inside the HTML <body> and </body> tags, TTSite will use the contents of |
783 | C<root/lib/site> to add the top and bottom. |
4d583dd8 |
784 | |
785 | First create a directory for book-related TT templates: |
786 | |
787 | $ mkdir root/src/books |
788 | |
789 | Then open C<root/src/books/list.tt2> in your editor and enter: |
790 | |
791 | [% # This is a TT comment. The '-' at the end "chomps" the newline. You won't -%] |
792 | [% # see this "chomping" in your browser because HTML ignores blank lines, but -%] |
793 | [% # it WILL eliminate a blank line if you view the HTML source. It's purely -%] |
794 | [%- # optional, but both the beginning and the ending TT tags support chomping. -%] |
795 | |
796 | [% # Provide a title to root/lib/site/header -%] |
797 | [% META title = 'Book List' -%] |
798 | |
799 | <table> |
800 | <tr><th>Title</th><th>Rating</th><th>Author(s)</th></tr> |
801 | [% # Display each book in a table row %] |
802 | [% FOREACH book IN books -%] |
803 | <tr> |
804 | <td>[% book.title %]</td> |
805 | <td>[% book.rating %]</td> |
806 | <td> |
14e6feb0 |
807 | [% # First initialize a TT variable to hold a list. Then use a TT FOREACH -%] |
808 | [% # loop in 'side effect notation' to load just the last names of the -%] |
7e5eb02c |
809 | [% # authors into the list. Note that we make a bogus assignment to the -%] |
810 | [% # 'unused' vbl to avoid printing the size of the list after each push. -%] |
14e6feb0 |
811 | [% tt_authors = [ ]; |
7e5eb02c |
812 | unused = tt_authors.push(author.last_name) FOREACH author = book.authors %] |
14e6feb0 |
813 | [% # Now use a TT 'virtual method' to display the author count in parens -%] |
814 | ([% tt_authors.size %]) |
815 | [% # Use another vmethod to join & print the names with comma separators -%] |
816 | [% tt_authors.join(', ') %] |
4d583dd8 |
817 | </td> |
818 | </tr> |
819 | [% END -%] |
820 | </table> |
821 | |
64ccd8a8 |
822 | As indicated by the inline comments above, the C<META title> line uses |
823 | TT's META feature to provide a title to C<root/lib/site/header>. |
824 | Meanwhile, the outer C<FOREACH> loop iterates through each C<book> model |
825 | object and prints the C<title> and C<rating> fields. An inner |
14e6feb0 |
826 | C<FOREACH> loop prints the last name of each author in a comma-separated |
827 | list within a single table cell. |
64ccd8a8 |
828 | |
71dedf57 |
829 | If you are new to TT, the C<[%> and C<%]> tags are used to delimit TT |
830 | code. TT supports a wide variety of directives for "calling" other |
64ccd8a8 |
831 | files, looping, conditional logic, etc. In general, TT simplifies the |
832 | usual range of Perl operators down to the single dot (C<.>) operator. |
833 | This applies to operations as diverse as method calls, hash lookups, and |
834 | list index values (see |
835 | L<http://www.template-toolkit.org/docs/default/Manual/Variables.html> |
836 | for details and examples). In addition to the usual C<Template> module |
837 | Pod documentation, you can access the TT manual at |
838 | L<http://www.template-toolkit.org/docs/default/>. |
839 | |
840 | B<NOTE>: The C<TTSite> helper creates several TT files using an |
841 | extension of C<.tt2>. Most other Catalyst and TT examples use an |
842 | extension of C<.tt>. You can use either extension (or no extension at |
843 | all) with TTSite and TT, just be sure to use the appropriate extension |
844 | for both the file itself I<and> the C<$c-E<gt>stash-E<gt>{template} = |
845 | ...> line in your controller. This document will use C<.tt2> for |
846 | consistency with the files already created by the C<TTSite> helper. |
4d583dd8 |
847 | |
848 | |
4d583dd8 |
849 | =head1 RUN THE APPLICATION |
850 | |
64ccd8a8 |
851 | First, let's enable an environment variable option that causes |
852 | DBIx::Class to dump the SQL statements it's using to access the database |
853 | (this option can provide extremely helpful troubleshooting information): |
4d583dd8 |
854 | |
855 | $ export DBIX_CLASS_STORAGE_DBI_DEBUG=1 |
856 | |
64ccd8a8 |
857 | B<NOTE>: You can also set this in your code using |
858 | C<$class-E<gt>storage-E<gt>debug(1);>. See |
71dedf57 |
859 | L<DBIx::Class::Manual::Troubleshooting> for details (including options |
860 | to log to file instead of displaying to the Catalyst development server |
861 | log). |
4d583dd8 |
862 | |
863 | Then run the Catalyst "demo server" script: |
864 | |
865 | $ script/myapp_server.pl |
866 | |
867 | You should get something like this: |
868 | |
869 | $ script/myapp_server.pl |
870 | [Tue May 16 12:51:33 2006] [catalyst] [debug] Debug messages enabled |
871 | [Tue May 16 12:51:33 2006] [catalyst] [debug] Loaded plugins: |
872 | .------------------------------------------------------------------------------. |
14e6feb0 |
873 | | Catalyst::Plugin::ConfigLoader 0.09 | |
4d583dd8 |
874 | | Catalyst::Plugin::Static::Simple 0.14 | |
4d583dd8 |
875 | | Catalyst::Plugin::StackTrace 0.04 | |
876 | | Catalyst::Plugin::DefaultEnd 0.06 | |
877 | '------------------------------------------------------------------------------' |
878 | |
879 | [Tue May 16 12:51:33 2006] [catalyst] [debug] Loaded dispatcher "Catalyst::Dispatcher" |
880 | [Tue May 16 12:51:33 2006] [catalyst] [debug] Loaded engine "Catalyst::Engine::HTTP" |
881 | [Tue May 16 12:51:33 2006] [catalyst] [debug] Found home "/home/me/MyApp" |
882 | [Tue May 16 12:51:37 2006] [catalyst] [debug] Loaded components: |
883 | .-------------------------------------------------------------------+----------. |
884 | | Class | Type | |
885 | +-------------------------------------------------------------------+----------+ |
886 | | MyApp::Controller::Books | instance | |
887 | | MyApp::Controller::Root | instance | |
888 | | MyApp::Model::MyAppDB | instance | |
889 | | MyApp::Model::MyAppDB::Author | class | |
890 | | MyApp::Model::MyAppDB::Book | class | |
891 | | MyApp::Model::MyAppDB::BookAuthor | class | |
892 | | MyApp::View::TT | instance | |
893 | '-------------------------------------------------------------------+----------' |
894 | |
895 | [Tue May 16 12:51:37 2006] [catalyst] [debug] Loaded Private actions: |
896 | .----------------------+----------------------------------------+--------------. |
897 | | Private | Class | Method | |
898 | +----------------------+----------------------------------------+--------------+ |
899 | | /default | MyApp::Controller::Root | default | |
900 | | /end | MyApp | end | |
901 | | /books/list | MyApp::Controller::Books | list | |
902 | '----------------------+----------------------------------------+--------------' |
903 | |
904 | [Tue May 16 12:51:37 2006] [catalyst] [debug] Loaded Path actions: |
905 | .--------------------------------------+---------------------------------------. |
906 | | Path | Private | |
907 | +--------------------------------------+---------------------------------------+ |
908 | | /books/list | /books/list | |
909 | '--------------------------------------+---------------------------------------' |
910 | |
911 | [Tue May 16 12:51:37 2006] [catalyst] [info] MyApp powered by Catalyst 5.6902 |
912 | You can connect to your server at http://localhost:3000 |
913 | |
914 | Some things you should note in the output above: |
915 | |
916 | =over 4 |
917 | |
918 | =item * |
919 | |
64ccd8a8 |
920 | Catalyst::Model::DBIC::Schema took our C<MyAppDB::Book> and made it |
921 | C<MyApp::Model::MyAppDB::Book> (and similar actions were performed on |
922 | C<MyAppDB::Author> and C<MyAppDB::BookAuthor>). |
4d583dd8 |
923 | |
924 | =item * |
925 | |
64ccd8a8 |
926 | The "list" action in our Books controller showed up with a path of |
927 | C</books/list>. |
4d583dd8 |
928 | |
929 | =back |
930 | |
64ccd8a8 |
931 | Point your browser to L<http://localhost:3000> and you should still get |
932 | the Catalyst welcome page. |
4d583dd8 |
933 | |
64ccd8a8 |
934 | Next, to view the book list, change the URL in your browser to |
935 | L<http://localhost:3000/books/list>. You should get a list of the five |
936 | books loaded by the C<myapp01.sql> script above, with TTSite providing |
937 | the formatting for the very simple output we generated in our template. |
938 | The count and space-separated list of author last names appear on the |
939 | end of each row. |
4d583dd8 |
940 | |
64ccd8a8 |
941 | Also notice in the output of the C<script/myapp_server.pl> that DBIC |
942 | used the following SQL to retrieve the data: |
4d583dd8 |
943 | |
944 | SELECT me.id, me.title, me.rating FROM books me |
945 | |
64ccd8a8 |
946 | Along with a list of the following commands to retrieve the authors for |
947 | each book (the lines have been "word wrapped" here to improve |
948 | legibility): |
4d583dd8 |
949 | |
950 | SELECT author.id, author.first_name, author.last_name |
951 | FROM book_authors me |
952 | JOIN authors author ON ( author.id = me.author_id ) |
953 | WHERE ( me.book_id = ? ): `1' |
954 | |
64ccd8a8 |
955 | You should see 10 such lines of debug output, two for each of the five |
956 | author_id values (it pulls the data once for the count logic and another |
957 | time to actually display the list). |
4d583dd8 |
958 | |
959 | |
960 | =head1 AUTHOR |
961 | |
962 | Kennedy Clark, C<hkclark@gmail.com> |
963 | |
964 | Please report any errors, issues or suggestions to the author. |
965 | |
71dedf57 |
966 | Copyright 2006, Kennedy Clark, under Creative Commons License |
967 | (L<http://creativecommons.org/licenses/by-nc-sa/2.5/>). |
4d583dd8 |
968 | |