=back
-B<TIP>: Note that all of the code for this part of the tutorial can be
-pulled from the Catalyst Subversion repository in one step with the
-following command:
-
- svn co http://dev.catalyst.perl.org/repos/Catalyst/tags/examples/Tutorial/MyApp/5.7/CatalystBasics MyApp
-
+You can checkout the source code for this example from the catalyst
+subversion repository as per the instructions in
+L<Catalyst::Manual::Tutorial::Intro>
=head1 CREATE A CATALYST PROJECT
[debug] Debug messages enabled
[debug] Loaded plugins:
.----------------------------------------------------------------------------.
- | Catalyst::Plugin::ConfigLoader 0.06 |
+ | Catalyst::Plugin::ConfigLoader 0.13 |
| Catalyst::Plugin::Static::Simple 0.14 |
'----------------------------------------------------------------------------'
[debug] Loaded dispatcher "Catalyst::Dispatcher"
[debug] Loaded engine "Catalyst::Engine::HTTP"
- [debug] Found home "/root/dev/MyApp"
+ [debug] Found home "/home/me/MyApp"
+ [debug] Loaded Config "/home/me/myapp.yml"
[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class | Type |
| /end | MyApp::Controller::Root | end |
'----------------------+--------------------------------------+--------------'
- [info] MyApp powered by Catalyst 5.7000
- You can connect to your server at http://localhost.localdomain:3000
+ [info] MyApp powered by Catalyst 5.7002
+ You can connect to your server at http://localhost:3000
B<NOTE>: Be sure you run the C<script/myapp_server.pl> command from the
'base' directory of your application, not inside the C<script> directory
INSERT INTO authors VALUES (4, 'Richard', 'Stevens');
INSERT INTO authors VALUES (5, 'Douglas', 'Comer');
INSERT INTO authors VALUES (6, 'Tom', 'Christiansen');
- INSERT INTO authors VALUES (7, ' Nathan', 'Torkington');
+ INSERT INTO authors VALUES (7, 'Nathan', 'Torkington');
INSERT INTO authors VALUES (8, 'Jeffrey', 'Zeldman');
INSERT INTO book_authors VALUES (1, 1);
INSERT INTO book_authors VALUES (1, 2);
as "DBIC") has rapidly emerged as the Perl-based ORM technology of choice.
Most new Catalyst applications rely on DBIC, as will this tutorial.
-Note: See L<Catalyst:: Model::CDBI> for more information on using
+Note: See L<Catalyst::Model::CDBI> for more information on using
Catalyst with L<Class::DBI|Class::DBI>.
=head2 Create a DBIC Schema File
load the model object for the C<books>, C<book_authors>, and C<authors>
tables created in the previous step.
-Open C<lib/MyAppDB.pm> in your editor and insert:
+Create C<lib/MyAppDB.pm> in your editor and insert:
package MyAppDB;
of the package where it is used. Therefore, in C<MyAppDB.pm>,
C<__PACKAGE__> is equivalent to C<MyAppDB>.
+B<Note:> As with any Perl package, we need to end the last line with
+a statement that evaluates to C<true>. This is customarily done with
+C<1> on a line by itself as shown above.
+
=head2 Create the DBIC "Result Source" Files
$ mkdir lib/MyAppDB
-Then open C<lib/MyAppDB/Book.pm> in your editor and enter:
+Then create C<lib/MyAppDB/Book.pm> in your editor and enter:
package MyAppDB::Book;
Note that you cannot define a C<many_to_many> relationship without also
having the C<has_many> relationship in place.
-Next, open C<lib/MyAppDB/Author.pm> in your editor and enter:
+Next, create C<lib/MyAppDB/Author.pm> in your editor and enter:
package MyAppDB::Author;
1;
-Finally, open C<lib/MyAppDB/BookAuthor.pm> in your editor and enter:
+Finally, create C<lib/MyAppDB/BookAuthor.pm> in your editor and enter:
package MyAppDB::BookAuthor;
$c->stash->{books} = [$c->model('MyAppDB::Book')->all];
# Set the TT template to use. You will almost always want to do this
- # in your action methods (actions methods respond to user input in
+ # in your action methods (action methods respond to user input in
# your controllers).
$c->stash->{template} = 'books/list.tt2';
}
response output. Catalyst v5.7000 ships with a new mechanism,
L<Catalyst::Action::RenderView|Catalyst::Action::RenderView>, that
automatically performs this operation. If you look in
-C<lib/MyApp/Controller/Root.pm>, you should see the this empty
+C<lib/MyApp/Controller/Root.pm>, you should see the empty
definition for the C<sub end> method:
sub end : ActionClass('RenderView') {}
$ mkdir root/src/books
-Then open C<root/src/books/list.tt2> in your editor and enter:
+Then create C<root/src/books/list.tt2> in your editor and enter:
[% # This is a TT comment. The '-' at the end "chomps" the newline. You won't -%]
[% # see this "chomping" in your browser because HTML ignores blank lines, but -%]
[debug] Debug messages enabled
[debug] Loaded plugins:
.----------------------------------------------------------------------------.
- | Catalyst::Plugin::ConfigLoader 0.06 |
- | Catalyst::Plugin::StackTrace 0.04 |
+ | Catalyst::Plugin::ConfigLoader 0.13 |
+ | Catalyst::Plugin::StackTrace 0.06 |
| Catalyst::Plugin::Static::Simple 0.14 |
'----------------------------------------------------------------------------'
[debug] Loaded dispatcher "Catalyst::Dispatcher"
[debug] Loaded engine "Catalyst::Engine::HTTP"
[debug] Found home "/home/me/MyApp"
+ [debug] Loaded Config "/home/me/myapp.yml"
[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class | Type |
| /books/list | /books/list |
'-------------------------------------+--------------------------------------'
- [info] MyApp powered by Catalyst 5.7000
- You can connect to your server at http://localhost.localdomain:3000
+ [info] MyApp powered by Catalyst 5.7002
+ You can connect to your server at http://localhost:3000
Some things you should note in the output above:
By default, C<Catalyst::View::TT> will look for a template that uses the
same name as your controller action, allowing you to save the step of
manually specifying the template name in each action. For example, this
-would allow us to remove (or comment out) the
+would allow us to remove the
C<$c-E<gt>stash-E<gt>{template} = 'books/list.tt2';> line of our
C<list> action in the Books controller. Open
-C<lib/MyApp/Controller/Books.pm> in your editor and update it to
-match the following:
+C<lib/MyApp/Controller/Books.pm> in your editor and comment out this line
+to match the following (only the C<$c-E<gt>stash-E<gt>{template}> line
+has changed):
=head2 list
# stash where they can be accessed by the TT template
$c->stash->{books} = [$c->model('MyAppDB::Book')->all];
- # Automatically look for a template of 'books/list.tt2' template
- # (if TEMPLATE_EXTENSION is set to '.tt2')
+ # Set the TT template to use. You will almost always want to do this
+ # in your action methods (actions methods respond to user input in
+ # your controllers).
+ #$c->stash->{template} = 'books/list.tt2';
}
C<Catalyst::View::TT> defaults to looking for a template with no
previous section and access the L<http://localhost:3000/books/list>
as before.
-Although this can be a valuable technique to establish a default
-template for each of your actions, the remainder of the tutorial
-will manually assign the template name to
-C<$c-E<gt>stash-E<gt>{template}> in each action in order to make
-the logic as conspicuous as possible.
+B<NOTE:> Please note that if you use the default template technique,
+you will B<not> be able to use either the C<$c-E<gt>forward> or
+the C<$c-E<gt>detach> mechanisms (these are discussed in Part 2 and
+Part 8 of the Tutorial).
+
+
+=head1 RETURN TO A MANUALLY-SPECIFIED TEMPLATE
+
+In order to be able to use C<$c-E<gt>forward> and C<$c-E<gt>detach>
+later in the tutorial, you should remove the comment from the
+statement in C<sub list>:
+
+ $c->stash->{template} = 'books/list.tt2';
+
+Then delete the C<TEMPLATE_EXTENSION> line in
+C<lib/MyApp/View/TT.pm>.
+
+You should then be able to restart the development server and
+access L<http://localhost:3000/books/list> in the same manner as
+with earlier sections.
=head1 AUTHOR