Catalyst-Manual
view release on metacpan or search on metacpan
lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod view on Meta::CPAN
=back
=item *
Middle
=over 4
=item *
Link to previous part of the chain with C<:Chained('_name_')>
=item *
Get arguments through C<CaptureArgs()>
=item *
Specify the path to match with C<PathPart()>
=back
=item *
End
=over 4
=item *
Link to previous part of the chain with C<:Chained('_name_')>
=item *
B<< Do NOT get arguments through "C<CaptureArgs()>," use "C<Args()>" instead to end a chain >>
=item *
Specify the path to match with C<PathPart()>
=back
=back
In our C<url_create> method above, we have combined all three parts into
a single method: C<:Chained('/')> to start the chain,
C<:PathPart('books/url_create')> to specify the base URL to match, and
C<:Args(3)> to capture exactly three arguments and to end the chain.
As we will see shortly, a chain can consist of as many "links" as you
wish, with each part capturing some arguments and doing some work along
the way. We will continue to use the Chained action type in this
chapter of the tutorial and explore slightly more advanced capabilities
with the base method and delete feature below. But Chained dispatch is
capable of far more. For additional information, see
L<Catalyst::Manual::Intro/Action types>,
L<Catalyst::DispatchType::Chained>, and the 2006 Advent calendar entry
on the subject: L<http://www.catalystframework.org/calendar/2006/10>.
=head2 Try the Chained Action
If you look back at the development server startup logs from your
initial version of the C<url_create> method (the one using the C<:Local>
attribute), you will notice that it produced output similar to the
following:
[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path | Private |
+-------------------------------------+--------------------------------------+
| / | /default |
| / | /index |
| /books | /books/index |
| /books/list | /books/list |
| /books/url_create | /books/url_create |
'-------------------------------------+--------------------------------------'
When the development server restarts after our conversion to Chained
dispatch, the debug output should change to something along the lines of
the following:
[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path | Private |
+-------------------------------------+--------------------------------------+
| / | /default |
| / | /index |
| /books | /books/index |
| /books/list | /books/list |
'-------------------------------------+--------------------------------------'
[debug] Loaded Chained actions:
.-------------------------------------+--------------------------------------.
| Path Spec | Private |
+-------------------------------------+--------------------------------------+
| /books/url_create/*/*/* | /books/url_create |
'-------------------------------------+--------------------------------------'
C<url_create> has disappeared from the "Loaded Path actions" section but
it now shows up under the newly created "Loaded Chained actions"
section. And the "/*/*/*" portion clearly shows our requirement for
three arguments.
As with our non-chained version of C<url_create>, use your browser to
enter the following URL:
http://localhost:3000/books/url_create/TCPIP_Illustrated_Vol-2/5/4
You should see the same "Added book 'TCPIP_Illustrated_Vol-2' by
'Stevens' with a rating of 5." along with a dump of the new book model
object. Click the "Return to list" link, and you should find that there
are now seven books shown (two copies of I<TCPIP_Illustrated_Vol-2>).
=head2 Refactor to Use a 'base' Method to Start the Chains
Let's make a quick update to our initial Chained action to show a little
( run in 0.805 second using v1.01-cache-2.11-cpan-39bf76dae61 )