X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F04_BasicCRUD.pod;h=f19da1ee59fd102bbaa48e227df4aefe4b9dc1b3;hp=e63500dc7c53db584f11d4d5bf5bf2bc5db754b8;hb=b3876d9eb98d14bd0b30e58c760fa7e4bcd3eaab;hpb=2a6eb5f9e3b1b3a8dacd724bb8ab87ba18f733a5 diff --git a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod index e63500d..f19da1e 100644 --- a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod @@ -56,34 +56,35 @@ L =head1 DESCRIPTION -This chapter of the tutorial builds on the fairly primitive -application created in Chapter 3 to add basic support for Create, -Read, Update, and Delete (CRUD) of C objects. Note that the -'list' function in Chapter 2 already implements the Read portion of -CRUD (although Read normally refers to reading a single object; you -could implement full Read functionality using the techniques -introduced below). This section will focus on the Create and Delete -aspects of CRUD. More advanced capabilities, including full Update -functionality, will be addressed in Chapter 9. - -Although this chapter of the tutorial will show you how to build CRUD -functionality yourself, another option is to use a "CRUD builder" type -of tool to automate the process. You get less control, but it's quick -and easy. For example, see -L, -L, and -L. +This chapter of the tutorial builds on the fairly primitive application +created in +L to add +basic support for Create, Read, Update, and Delete (CRUD) of C +objects. Note that the 'list' function in +L already +implements the Read portion of CRUD (although Read normally refers to +reading a single object; you could implement full Read functionality +using the techniques introduced below). This section will focus on the +Create and Delete aspects of CRUD. More advanced capabilities, +including full Update functionality, will be addressed in +L. + +Although this chapter of the tutorial will show you how to build CRUD +functionality yourself, another option is to use a "CRUD builder" type +of tool to automate the process. You get less control, but it can be +quick and easy. For example, see L, +L, and L. You can check out the source code for this example from the Catalyst Subversion repository as per the instructions in -L. +L. =head1 FORMLESS SUBMISSION -Our initial attempt at object creation will utilize the "URL -arguments" feature of Catalyst (we will employ the more common form- -based submission in the sections that follow). +Our initial attempt at object creation will utilize the "URL arguments" +feature of Catalyst (we will employ the more common form-based +submission in the sections that follow). =head2 Include a Create Action in the Books Controller @@ -96,7 +97,7 @@ Edit C and enter the following method: =cut - sub url_create : Local { + sub url_create :Local { # In addition to self & context, get the title, rating, & # author_id args from the URL. Note that Catalyst automatically # puts extra information after the "// and enter the following method: # Note: Above is a shortcut for this: # $book->create_related('book_authors', {author_id => $author_id}); - # Assign the Book object to the stash for display in the view - $c->stash->{book} = $book; + # Assign the Book object to the stash for display and set template + $c->stash(book => $book, + template => 'books/create_done.tt2'); - # Set the TT template to use - $c->stash->{template} = 'books/create_done.tt2'; + # Disable caching for this page + $c->response->header('Cache-Control' => 'no-cache'); } Notice that Catalyst takes "extra slash-separated information" from the -URL and passes it as arguments in C<@_>. The C action then -uses a simple call to the DBIC C method to add the requested -information to the database (with a separate call to -C to update the join table). As do virtually all -controller methods (at least the ones that directly handle user input), -it then sets the template that should handle this request. +URL and passes it as arguments in C<@_> (as long as the number of +arguments is not "fixed" using an attribute like C<:Args(0)>). The +C action then uses a simple call to the DBIC C +method to add the requested information to the database (with a separate +call to C to update the join table). As do +virtually all controller methods (at least the ones that directly handle +user input), it then sets the template that should handle this request. + +Also note that we are explicitly setting a C "Cache-Control" +header to force browsers using the page to get a fresh copy every time. +You could even move this to a C method in +C and it would automatically get applied +to every page in the whole application via a single line of code +(remember from Chapter 3, that every C method gets run in the +Controller hierarchy). =head2 Include a Template for the 'url_create' Action: @@ -142,23 +153,24 @@ Edit C and then enter: [% USE Dumper(Indent=1) -%] [% # Set the page title. META can 'go back' and set values in templates -%] - [% # that have been processed 'before' this template (here it's for -%] - [% # root/lib/site/html and root/lib/site/header). Note that META only -%] - [% # works on simple/static strings (i.e. there is no variable -%] - [% # interpolation). -%] + [% # that have been processed 'before' this template (here it's updating -%] + [% # the title in the root/src/wrapper.tt2 wrapper template). Note that -%] + [% # META only works on simple/static strings (i.e. there is no variable -%] + [% # interpolation -- if you need dynamic/interpolated content in your -%] + [% # title, set "$c->stash(title => $something)" in the controller). -%] [% META title = 'Book Created' %] [% # Output information about the record that was added. First title. -%]

Added book '[% book.title %]' - [% # Output the last name of the first author. -%] + [% # Then, output the last name of the first author -%] by '[% book.authors.first.last_name %]' - [% # Output the rating for the book that was added -%] + [% # Then, output the rating for the book that was added -%] with a rating of [% book.rating %].

- [% # Provide a link back to the list page -%] - [% # 'uri_for()' builds a full URI; e.g., 'http://localhost:3000/books/list' -%] + [% # Provide a link back to the list page. 'c.uri_for' builds -%] + [% # a full URI; e.g., 'http://localhost:3000/books/list' -%]

Return to list

[% # Try out the TT Dumper (for development only!) -%] @@ -167,47 +179,23 @@ Edit C and then enter: [% Dumper.dump(book) %] -The TT C directive allows access to a variety of plugin modules -(TT plugins, that is, not Catalyst plugins) to add extra functionality -to the base TT capabilities. Here, the plugin allows -L "pretty printing" of objects and -variables. Other than that, the rest of the code should be familiar -from the examples in Chapter 3. - -Note: If you are using TT v2.15 you will need to change the code that -outputs the "last name for the first author" above to match this: - - [% authors = book.authors %] - by '[% authors.first.last_name IF authors.first; - authors.list.first.value.last_name IF ! authors.first %]' - -to get around an issue in TT v2.15 where blessed hash objects were not -handled correctly. But, if you are still using v2.15, it's probably -time to upgrade (v2.15 is exactly 3 years old on the day I'm typing -this). If you are following along in Debian, then you should be on at -least v2.20. You can test your version of Template Toolkit with the -following: - - perl -MTemplate -e 'print "$Template::VERSION\n"' +The TT C directive allows access to a variety of plugin modules (TT +plugins, that is, not Catalyst plugins) to add extra functionality to +the base TT capabilities. Here, the plugin allows L +"pretty printing" of objects and variables. Other than that, the rest +of the code should be familiar from the examples in Chapter 3. =head2 Try the 'url_create' Feature -If the application is still running from before, use C to kill -it. Then restart the server: +Make sure the development server is running with the "-r" restart +option: - $ DBIC_TRACE=1 script/myapp_server.pl + $ DBIC_TRACE=1 script/myapp_server.pl -r Note that new path for C appears in the startup debug output. -B: You can use C