X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2FMoreCatalystBasics.pod;h=e125cd748a9c8af0bc44b28088ecb19b64772b2b;hp=cc42b0731c5a1af7fc428168753a108c71876575;hb=4ab6212da7a5e07df9837b0e57fb2b0c37aa9759;hpb=191dee29949dbcb6c296feb845dc08e22eac2949 diff --git a/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod b/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod index cc42b07..e125cd7 100644 --- a/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod +++ b/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod @@ -1,11 +1,11 @@ =head1 NAME -Catalyst::Manual::Tutorial::MoreCatalystBasics - Catalyst Tutorial - Part 3: More Catalyst Application Development Basics +Catalyst::Manual::Tutorial::MoreCatalystBasics - Catalyst Tutorial - Chapter 3: More Catalyst Application Development Basics =head1 OVERVIEW -This is B for the Catalyst tutorial. +This is B for the Catalyst tutorial. L @@ -56,13 +56,13 @@ L =head1 DESCRIPTION -This part of the tutorial builds on the work done in Part 2 to explore -some features that are more typical of "real world" web applications. -From this part of the tutorial onward, we will be building a simple -book database application. Although the application will be too -limited to be of use to anyone, it should provide a basic environment -where we can explore a variety of features used in virtually all web -applications. +This chapter of the tutorial builds on the work done in Chapter 2 to +explore some features that are more typical of "real world" web +applications. From this chapter of the tutorial onward, we will be +building a simple book database application. Although the application +will be too limited to be of use to anyone, it should provide a basic +environment where we can explore a variety of features used in +virtually all web applications. You can checkout the source code for this example from the catalyst subversion repository as per the instructions in @@ -74,8 +74,8 @@ L. The remainder of the tutorial will build an application called C. First use the Catalyst C script to initialize the framework for the C application (make sure you aren't still inside the -directory of the C application from the previous part of the -tutorial): +directory of the C application from the previous chapter of the +tutorial or in a directory that already has a "MyApp" subdirectory): $ catalyst.pl MyApp created "MyApp" @@ -86,7 +86,7 @@ tutorial): created "MyApp/script/myapp_create.pl" $ cd MyApp -This creates a similar skeletal structure to what we saw in Part 2 of +This creates a similar skeletal structure to what we saw in Chapter 2 of the tutorial, except with C and C substituted for C and C. @@ -138,22 +138,23 @@ L file (versus having the values hard-coded inside your Perl modules). Config::General uses syntax very similar to Apache configuration files. We will see how to use this feature of Catalyst during the authentication and authorization -sections (Part 5 and Part 6). - -B If you are using a version of -L prior to version 1.06, you need to -be aware that Catalyst changed from a default format of YAML to the -more straightforward C format. This tutorial use the -newer C configuration file for C instead -of C for YAML. However, Catalyst has long supported both -formats and Catalyst will automatically use either C or -C (or any other format supported by -L and -L). If you are using a versions of -Catalyst::Devel prior to 1.06, you can convert to the newer format by -simply creating the C file manually and deleting -C. The default contents of C should only -consist of one line: C. +sections (Chapter 5 and Chapter 6). + +B If you are using a version of +L prior to version 1.06, be aware +that Catalyst changed the default format from YAML to the more +straightforward C style. This tutorial uses the +newer C file for C. However, Catalyst +supports both formats and will automatically use either C +or C (or any other format supported by +L and +L). If you are using a version of +Catalyst::Devel prior to 1.06, you can convert to the newer format by +simply creating the C file manually and deleting +C. The default contents of the C you create +should only consist of one line: + + name MyApp B: This script can be useful for converting between configuration formats: @@ -161,10 +162,6 @@ formats: perl -Ilib -e 'use MyApp; use Config::General; Config::General->new->save_file("myapp.conf", MyApp->config);' -B The default C should look like: - - name MyApp - =item * L @@ -174,21 +171,32 @@ as images and CSS files under the development server. =back -For out application, we want to add one new plugin into the mix. To +For our application, we want to add one new plugin into the mix. To do this, edit C (this file is generally referred to as -your I) and delete the line with: +your I) and delete the lines with: - __PACKAGE__->setup(qw/-Debug ConfigLoader Static::Simple/); + use Catalyst qw/-Debug + ConfigLoader + Static::Simple/; Then replace it with: - __PACKAGE__->setup(qw/ - -Debug - ConfigLoader - Static::Simple - - StackTrace - /); + # Load plugins + use Catalyst qw/-Debug + ConfigLoader + Static::Simple + + StackTrace + /; + +B Recent versions of C have used a variety of +techniques to load these plugins/flags. For example, you might see +the following: + + __PACKAGE__->setup(qw/-Debug ConfigLoader Static::Simple/); + +Don't let these variations confuse you -- they all accomplish the same +result. This tells Catalyst to start using one new plugin, L, to add a @@ -239,7 +247,7 @@ actions: created "/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm" created "/home/me/MyApp/script/../t/controller_Books.t" -Then edit C (as discussed in Part 2 of +Then edit C (as discussed in Chapter 2 of the Tutorial, Catalyst has a separate directory under C for each of the three parts of MVC: C, C, and C) and add the following method to the controller: @@ -279,31 +287,94 @@ Context object is automatically passed to all Catalyst components. It is used to pass information between components and provide access to Catalyst and plugin functionality. -B Catalyst actions are regular Perl methods, but they make use -of Nicholas Clark's C module (that's the "C<: Local>" next -to the C in the code above) to provide additional -information to the Catalyst dispatcher logic. Many newer Catalyst -applications are switching to the use of "Literal" C<:Path> actions -and C attribute in lieu of C<: Local> and C<: Private>. For -example, C can be used instead of C (because no path was supplied to C it matches -the "empty" URL in the namespace of that module... the same thing -C would do) or C could be -used instead of the C above (the C argument to -C would make it match on the URL C under C, the -namespace of the current module). See "Action Types" in -L as well as Part 5 -of this tutorial (Authentication) for additional information. Another -popular but more advanced feature is C actions that allow a -single URL to "chain together" multiple action method calls, each with -an appropriate number of arguments (see -L for -details). +Catalyst actions are regular Perl methods, but they make use of +attributes (the "C<: Local>" next to the "C" in the code +above) to provide additional information to the Catalyst dispatcher +logic (note that the space between the colon and the attribute name is +optional... you will see attributes written both ways). Most Catalyst +Controllers use one of five action types: + +=over 4 + +=item * + +B<:Private> -- Use C<:Private> for methods that you want to make into +an action, but you do not want Catalyst to directly expose the action +to your users. Catalyst will not map C<:Private> methods to a URI. +Use them for various sorts of "special" methods (the C, +C, etc. discussed below) or for methods you want to be able to +C or C to. (If the method is a plain old "helper +method" that you don't want to be an action at all, then just define +the method without any attribute -- you can call it in your code, but +the Catalyst dispatcher will ignore it.) + +There are five types of "special" build-in C<:Private> actions: +C, C, C, C, and C. + +=over 4 + +=item * + +With C, C, C, C private actions, only the +most specific action of each type will be called. For example, if you +define a C action in your controller it will I a +C action in your application/root controller -- I the +action in your controller will be called. + +=item * + +Unlike the other actions where only a single method is called for each +request, I auto action along the chain of namespaces will be +called. Each C action will be called I. + +=back + +=item * + +B<:Path> -- C<:Path> actions let you map a method to an explicit URI +path. For example, "C<:Path('list')>" in +C would match on the URL +C but "C<:Path('/list')>" would match +on C. You can use C<:Args()> to specify +how many arguments an action should except. See +L for more information and a few +examples. + +=item * + +B<:Local> -- C<:Local> is merely a shorthand for +"C<:Path('_name_of_method_')>". For example, these are equivalent: +"C" and +"C". + +=item * + +B<:Global> -- C<:Global> is merely a shorthand for +"C<:Path('/_name_of_method_')>". For example, these are equivalent: +"C" and +"C". + +=item * + +B<:Chained> -- Newer Catalyst applications tend to use the Chained +dispatch form of action types because of its power and flexibility. +It allows a series of controller methods to automatically be dispatched +to service a single user request. See +L +and L +for more information on chained actions. + +=back + +You should refer to L for +additional information and for coverage of some lesser-used action +types not discussed here (C and C). =head1 CATALYST VIEWS -As mentioned in Part 2 of the tutorial, views are where you render +As mentioned in Chapter 2 of the tutorial, views are where you render output, typically for display in the user's web browser (but also possibly using other display output-generation systems). The code in C selects the I of view to use, with the actual @@ -333,18 +404,20 @@ L =back -Both are similar, but C merely creates the C +Both helpers are similar. C creates the C file and leaves the creation of any hierarchical template organization entirely up to you. (It also creates a C file for testing; -test cases will be discussed in Part 8.) On the other hand, the -C helper creates a modular and hierarchical view layout with +test cases will be discussed in Chapter 8.) C, on the other hand, +creates a modular and hierarchical view layout with separate Template Toolkit (TT) files for common header and footer information, configuration values, a CSS stylesheet, and more. -While TTSite is useful to bootstrap a project, most in the Catalyst -community recommend that it's easier to learn both Catalyst and -Template Toolkit if you use the more basic TT approach. Consequently, -this tutorial will use "plain old TT." +While C was useful to bootstrap a project, its use is now +deprecated and to be considered historical. For most Catalyst +applications it adds redundant functionality and structure; many in the +Catalyst community recommend that it's easier to learn both Catalyst and +Template Toolkit if you use the more basic C approach. +Consequently, this tutorial will use "plain old TT." Enter the following command to enable the C style of view rendering for this tutorial: @@ -376,7 +449,7 @@ And update it to match: TEMPLATE_EXTENSION => '.tt2', # Set the location for TT files INCLUDE_PATH => [ - MyApp->path_to( 'root/src' ), + MyApp->path_to( 'root', 'src' ), ], ); @@ -385,7 +458,15 @@ quote. This changes the default extension for Template Toolkit from '.tt' to '.tt2' and changes the base directory for your template files from -C to C. +C to C. These changes from the default are done mostly +to facilitate the application we're developing in this tutorial; as with +most things Perl, there's more than one way to do it... + +B We will use C as the base directory for our +template files, which a full naming convention of +C. Another popular option is to +use C as the base (with a full filename pattern of +C). =head2 Create a TT Template Page @@ -524,7 +605,7 @@ can use the SQLite command line environment to do a quick dump of the database contents: $ sqlite3 myapp.db - SQLite version 3.4.2 + SQLite version 3.5.9 Enter ".help" for instructions sqlite> select * from books; 1|CCSP SNRS Exam Certification Guide|5 @@ -550,6 +631,9 @@ required if you do a single SQL statement on the command line). Use ".q" to exit from SQLite from the SQLite interactive mode and return to your OS command prompt. +For using other databases, such as PostgreSQL or MySQL, see +L. + =head1 DATABASE ACCESS WITH C @@ -571,7 +655,8 @@ Use the C model helper option to build a model that dynamically reads your database structure every time the application starts: - $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema create=dynamic dbi:SQLite:myapp.db + $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ + create=dynamic dbi:SQLite:myapp.db exists "/home/me/MyApp/script/../lib/MyApp/Model" exists "/home/me/MyApp/script/../t" exists "/home/me/MyApp/script/../lib/MyApp" @@ -580,15 +665,37 @@ starts: created "/home/me/MyApp/script/../t/model_DB.t" +The C