You've read the tutorial but you still can't seem to get a grasp on what exactly to use Chained dispatching for.

Chained is just another method of method/url dispatching that Catalyst allows you to use. Chained, however, allows you to chain together methods that need to share information across requests....

Say you want to construct a url that looks something like this: Normally, you'd have to go through a bunch of parsing involving $ENV{'PATH_PART'} $ENV{'PATH_INFO}, which is ugly, and hackish. You can do it like this with Chained:

# step one: # set up a base method to make sure your item exists, and # store your item information in $c->stash sub base : Chained('/') PathPart('item') CaptureArgs(1) { my ($self, $c, $itemid) = @_; my $item = $c->model('Item')->find($itemid); if ( $item == undef ) { $c->stash( error_msg => "This item does not exist" ); $c->detach("nonexistent"); } else { $c->stash( item => $item ); } }

Let's break this method down, and then we'll get to the rest of this chain.

sub base : Chained('/') PathPart('item') CaptureArgs(1)

the Chained('/') attribute sets up the base of the chain.

PathPart determines the name of the path to capture.

CaptureArgs tells Chained to capture one argument directly after the path part.

Now, let's finish the chain so we can have a working URL.

## the next part is to link the end point to the chain ## thisis done by passing the name of the base chain method to Chaine +d for this method sub view : Chained('base') PathPart('view') Args(0) { my ($self, $c) = @_; my $item = $c->stash->{item}; $c->stash( template => 'item/view.tt2', item => $item, ); }

This method is fairly self explanatory, but just to make sure, we'll go ahead and go over it.

As in the comments, Chained is passed the name of the base point of the chain, which in this case is 'base'. We determine the path part with PathPart once again, which can be noted that it doesn't have to match the current method's name. The one different attribute is the Args(0) attribute. Args tells Chained that the end of the chain has been reached, thus giving you a usable URL at this point. You can specify arguments to be passed in Args(), and they can be accessed via the @_ variable, like $self and $c.

To recap, this gives us a url like You can expand this for CRUD actions so you can have URLs like the above, as well as something like /item/create. Here is some example code:

sub blog : Chained('/') PathPart('blog') CaptureArgs(0) { } sub load_blog : Chained('blog') PathPart('') CaptureArgs(1) { my ($self, $c, $id) = @_; my $entry = $c->model('DB::Blog')->find($id); if ( $entry ) { $c->stash(entry => $entry ); } else { $c->stash( error_msg => "No such blog" ); $c->detach; } } sub view : Chained('load_blog') PathPart('view') Args(0) { my ($self, $c) = @_; } sub create : Chained('blog') PathPart('new') Args(0) FormConfig('blog +s/create.yml') { my ($self, $c) = @_; my $form = $c->stash->{form}; if ( $form->submitted_and_valid ) { my $entry = $c->model('DB::Blogs')->new_result({ created => D +ateTime->now }); $form->model->update($entry); $c->stash( status_msg => "Post saved", entry => $entry ); } else { $c->stash( error_msg => "Your form had errors" ); $c->detach; } }

This actually creates a couple different Chained instances, giving us the ability to have urls like /blog/id/view, blog/new, etc.

Next time, using $c->uri_for_action to create URIs for Chained methods.


In reply to Catalyst: Using Chained for URL Dispatching and Sane Method Organization by stonecolddevin

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.