File Coverage

blib/lib/Catalyst/DispatchType/Chained.pm
Criterion Covered Total %
statement 207 222 93.2
branch 94 120 78.3
condition 43 56 76.7
subroutine 17 17 100.0
pod 6 6 100.0
total 367 421 87.1


line stmt bran cond sub pod time code
1              
2             use Moose;
3 102     102   83061 extends 'Catalyst::DispatchType';
  102         304  
  102         951  
4              
5             use Text::SimpleTable;
6 102     102   730965 use Catalyst::ActionChain;
  102         268  
  102         3117  
7 102     102   45403 use Catalyst::Utils;
  102         407  
  102         3923  
8 102     102   941 use URI;
  102         263  
  102         2495  
9 102     102   601 use Scalar::Util ();
  102         243  
  102         2006  
10 102     102   579 use Encode 2.21 'decode_utf8';
  102         240  
  102         2324  
11 102     102   561  
  102         2634  
  102         16277  
12             has _endpoints => (
13             is => 'rw',
14             isa => 'ArrayRef',
15             required => 1,
16             default => sub{ [] },
17             );
18              
19             has _actions => (
20             is => 'rw',
21             isa => 'HashRef',
22             required => 1,
23             default => sub{ {} },
24             );
25              
26             has _children_of => (
27             is => 'rw',
28             isa => 'HashRef',
29             required => 1,
30             default => sub{ {} },
31             );
32              
33             no Moose;
34 102     102   845  
  102         310  
  102         725  
35             # please don't perltidy this. hairy code within.
36              
37             =head1 NAME
38              
39             Catalyst::DispatchType::Chained - Path Part DispatchType
40              
41             =head1 SYNOPSIS
42              
43             Path part matching, allowing several actions to sequentially take care of processing a request:
44              
45             # root action - captures one argument after it
46             sub foo_setup : Chained('/') PathPart('foo') CaptureArgs(1) {
47             my ( $self, $c, $foo_arg ) = @_;
48             ...
49             }
50              
51             # child action endpoint - takes one argument
52             sub bar : Chained('foo_setup') Args(1) {
53             my ( $self, $c, $bar_arg ) = @_;
54             ...
55             }
56              
57             =head1 DESCRIPTION
58              
59             Dispatch type managing default behaviour. For more information on
60             dispatch types, see:
61              
62             =over 4
63              
64             =item * L<Catalyst::Manual::Intro> for how they affect application authors
65              
66             =item * L<Catalyst::DispatchType> for implementation information.
67              
68             =back
69              
70             =head1 METHODS
71              
72             =head2 $self->list($c)
73              
74             Debug output for Path Part dispatch points
75              
76             =cut
77              
78             my ( $self, $c ) = @_;
79              
80 2     2 1 9 return unless $self->_endpoints;
81              
82 2 50       66 my $avail_width = Catalyst::Utils::term_width() - 9;
83             my $col1_width = ($avail_width * .50) < 35 ? 35 : int($avail_width * .50);
84 2         12 my $col2_width = $avail_width - $col1_width;
85 2 50       17 my $paths = Text::SimpleTable->new(
86 2         6 [ $col1_width, 'Path Spec' ], [ $col2_width, 'Private' ],
87 2         22 );
88              
89             my $has_unattached_actions;
90             my $unattached_actions = Text::SimpleTable->new(
91 2         197 [ $col1_width, 'Private' ], [ $col2_width, 'Missing parent' ],
92 2         10 );
93              
94             ENDPOINT: foreach my $endpoint (
95             sort { $a->reverse cmp $b->reverse }
96 2         140 @{ $self->_endpoints }
97 4         98 ) {
98 2         66 my $args = $endpoint->list_extra_info->{Args};
99              
100 5         16665 my @parts;
101             if($endpoint->has_args_constraints) {
102 5         14 @parts = map { "{$_}" } $endpoint->all_args_constraints;
103 5 100       166 } elsif(defined $endpoint->attributes->{Args}) {
    50          
104 2         63 @parts = (defined($endpoint->attributes->{Args}[0]) ? (("*") x $args) : '...');
  2         10  
105             }
106 3 50       74  
107             my @parents = ();
108             my $parent = "DUMMY";
109 5         77 my $extra = $self->_list_extra_http_methods($endpoint);
110 5         10 my $consumes = $self->_list_extra_consumes($endpoint);
111 5         17 my $scheme = $self->_list_extra_scheme($endpoint);
112 5         16 my $curr = $endpoint;
113 5         15 while ($curr) {
114 5         10 if (my $cap = $curr->list_extra_info->{CaptureArgs}) {
115 5         19 if($curr->has_captures_constraints) {
116 11 100       30 my $names = join '/', map { "{$_}" } $curr->all_captures_constraints;
117 1 50       32 unshift(@parts, $names);
118 1         46 } else {
  1         5  
119 1         36 unshift(@parts, (("*") x $cap));
120             }
121 0         0 }
122             if (my $pp = $curr->attributes->{PathPart}) {
123             unshift(@parts, $pp->[0])
124 11 50       265 if (defined $pp->[0] && length $pp->[0]);
125 11 100 66     56 }
126             $parent = $curr->attributes->{Chained}->[0];
127             $curr = $self->_actions->{$parent};
128 11         314 unshift(@parents, $curr) if $curr;
129 11         291 }
130 11 100       30 if ($parent ne '/') {
131             $has_unattached_actions = 1;
132 5 50       18 $unattached_actions->row('/' . ($parents[0] || $endpoint)->reverse, $parent);
133 0         0 next ENDPOINT;
134 0   0     0 }
135 0         0 my @rows;
136             foreach my $p (@parents) {
137 5         10 my $name = "/${p}";
138 5         12  
139 6         22 if (defined(my $extra = $self->_list_extra_http_methods($p))) {
140             $name = "${extra} ${name}";
141 6 50       24 }
142 0         0 if (defined(my $cap = $p->list_extra_info->{CaptureArgs})) {
143             if($p->has_captures_constraints) {
144 6 50       18 my $tc = join ',', @{$p->captures_constraints};
145 6 100       199 $name .= " ($tc)";
146 1         4 } else {
  1         24  
147 1         32 $name .= " ($cap)";
148             }
149 5         17 }
150             if (defined(my $ct = $p->list_extra_info->{Consumes})) {
151             $name .= ' :'.$ct;
152 6 50       21 }
153 0         0 if (defined(my $s = $p->list_extra_info->{Scheme})) {
154             $scheme = uc $s;
155 6 50       32 }
156 0         0  
157             unless ($p eq $parents[0]) {
158             $name = "-> ${name}";
159 6 100       23 }
160 1         4 push(@rows, [ '', $name ]);
161             }
162 6         37  
163             my $endpoint_arg_info = $endpoint;
164             if($endpoint->has_args_constraints) {
165 5         12 my $tc = join ',', @{$endpoint->args_constraints};
166 5 100       178 $endpoint_arg_info .= " ($tc)";
167 2         5 } else {
  2         45  
168 2         61 $endpoint_arg_info .= defined($endpoint->attributes->{Args}[0]) ? " ($args)" : " (...)";
169             }
170 3 50       78 push(@rows, [ '', (@rows ? "=> " : '').($extra ? "$extra " : ''). ($scheme ? "$scheme: ":'')."/${endpoint_arg_info}". ($consumes ? " :$consumes":"" ) ]);
171             my @display_parts = map { $_ =~s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; decode_utf8 $_ } @parts;
172 5 50       45 $rows[0][0] = join('/', '', @display_parts) || '/';
    50          
    50          
    50          
173 5         14 $paths->row(@$_) for @rows;
  16         146  
  0         0  
  16         126  
174 5   50     46 }
175 5         29  
176             $c->log->debug( "Loaded Chained actions:\n" . $paths->draw . "\n" );
177             $c->log->debug( "Unattached Chained actions:\n", $unattached_actions->draw . "\n" )
178 2         371 if $has_unattached_actions;
179 2 50       410 }
180              
181             my ( $self, $action ) = @_;
182             return unless defined $action->list_extra_info->{HTTP_METHODS};
183             return join(', ', @{$action->list_extra_info->{HTTP_METHODS}});
184 11     11   28  
185 11 50       27 }
186 0         0  
  0         0  
187             my ( $self, $action ) = @_;
188             return unless defined $action->list_extra_info->{CONSUMES};
189             return join(', ', @{$action->list_extra_info->{CONSUMES}});
190             }
191 5     5   11  
192 5 50       13 my ( $self, $action ) = @_;
193 0         0 return unless defined $action->list_extra_info->{Scheme};
  0         0  
194             return uc $action->list_extra_info->{Scheme};
195             }
196              
197 5     5   11 =head2 $self->match( $c, $path )
198 5 50       15  
199 0         0 Calls C<recurse_match> to see if a chain matches the C<$path>.
200              
201             =cut
202              
203             my ( $self, $c, $path ) = @_;
204              
205             my $request = $c->request;
206             return 0 if @{$request->args};
207              
208             my @parts = split('/', $path);
209 757     757 1 2145  
210             my ($chain, $captures, $parts) = $self->recurse_match($c, '/', \@parts);
211 757         18317  
212 757 100       1595 if ($parts && @$parts) {
  757         2456  
213             for my $arg (@$parts) {
214 557         2624 $arg =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
215             push @{$request->args}, $arg;
216 557         2739 }
217             }
218 557 100 100     2583  
219 80         265 return 0 unless $chain;
220 100         321  
  24         104  
221 100         200 my $action = Catalyst::ActionChain->from_chain($chain);
  100         321  
222              
223             $request->action("/${action}");
224             $request->match("/${action}");
225 557 100       2589 $request->captures($captures);
226             $c->action($action);
227 240         2559 $c->namespace( $action->namespace );
228              
229 240         1658 return 1;
230 240         951 }
231 240         6841  
232 240         6664 =head2 $self->recurse_match( $c, $parent, \@path_parts )
233 240         6807  
234             Recursive search for a matching chain.
235 240         1474  
236             =cut
237              
238             my ( $self, $c, $parent, $path_parts ) = @_;
239             my $children = $self->_children_of->{$parent};
240             return () unless $children;
241             my $best_action;
242             my @captures;
243             TRY: foreach my $try_part (sort { length($b) <=> length($a) }
244             keys %$children) {
245 1192     1192 1 3317 # $b then $a to try longest part first
246 1192         37561 my @parts = @$path_parts;
247 1192 50       3197 if (length $try_part) { # test and strip PathPart
248 1192         2527 next TRY unless
249             ($try_part eq join('/', # assemble equal number of parts
250 1192         13015 splice( # and strip them off @parts as well
  80141         115711  
251             @parts, 0, scalar(@{[split('/', $try_part)]})
252             ))); # @{[]} to avoid split to @_
253 19690         42652 }
254 19690 100       35786 my @try_actions = @{$children->{$try_part}};
255             TRY_ACTION: foreach my $action (@try_actions) {
256              
257              
258 19079 100       26838 if (my $capture_attr = $action->attributes->{CaptureArgs}) {
  19079         72787  
259             my $capture_count = $action->number_of_captures|| 0;
260              
261 1188         2521 # Short-circuit if not enough remaining parts
  1188         3941  
262 1188         2520 next TRY_ACTION unless @parts >= $capture_count;
263              
264             my @captures;
265 1559 100       45879 my @parts = @parts; # localise
266 676   100     19372  
267             # strip CaptureArgs into list
268             push(@captures, splice(@parts, 0, $capture_count));
269 676 100       2088  
270             # check if the action may fit, depending on a given test by the app
271 657         1101 next TRY_ACTION unless $action->match_captures($c, \@captures);
272 657         1755  
273             # try the remaining parts against children of this action
274             my ($actions, $captures, $action_parts, $n_pathparts) = $self->recurse_match(
275 657         1540 $c, '/'.$action->reverse, \@parts
276             );
277             # No best action currently
278 657 100       2547 # OR The action has less parts
279             # OR The action has equal parts but less captured data (ergo more defined)
280             if ($actions &&
281 635         17446 (!$best_action ||
282             $#$action_parts < $#{$best_action->{parts}} ||
283             ($#$action_parts == $#{$best_action->{parts}} &&
284             $#$captures < $#{$best_action->{captures}} &&
285             $n_pathparts > $best_action->{n_pathparts}))) {
286             my @pathparts = split /\//, $action->attributes->{PathPart}->[0];
287 635 100 100     3069 $best_action = {
      100        
288             actions => [ $action, @$actions ],
289             captures=> [ @captures, @$captures ],
290             parts => $action_parts,
291             n_pathparts => scalar(@pathparts) + $n_pathparts,
292             };
293 292         8221 }
294 292         3997 }
295             else {
296             {
297             local $c->req->{arguments} = [ @{$c->req->args}, @parts ];
298             next TRY_ACTION unless $action->match($c);
299             }
300             my $args_attr = $action->attributes->{Args}->[0];
301             my $args_count = $action->comparable_arg_number;
302             my @pathparts = split /\//, $action->attributes->{PathPart}->[0];
303             # No best action currently
304 883         1706 # OR This one matches with fewer parts left than the current best action,
  883         1664  
  883         3271  
305 883 100       3810 # And therefore is a better match
306             # OR No parts and this expects 0
307 289         7841 # The current best action might also be Args(0),
308 289         1053 # but we couldn't chose between then anyway so we'll take the last seen
309 289         7469 if (
310             !$best_action ||
311             @parts < @{$best_action->{parts}} ||
312             (
313             !@parts &&
314             defined($args_attr) &&
315             (
316 289 100 66     3108 $args_count eq "0" &&
      66        
      66        
      66        
      100        
      100        
317             (
318 34         443 ($c->config->{use_chained_args_0_special_case}||0) ||
319             (
320             exists($best_action->{args_count}) && defined($best_action->{args_count}) ?
321             ($best_action->{args_count} ne 0) : 1
322             )
323             )
324             )
325             )
326             ){
327             $best_action = {
328             actions => [ $action ],
329             captures=> [],
330             parts => \@parts,
331             args_count => $args_count,
332             n_pathparts => scalar(@pathparts),
333             };
334 257         2346 }
335             }
336             }
337             }
338             return @$best_action{qw/actions captures parts n_pathparts/} if $best_action;
339             return ();
340             }
341              
342             =head2 $self->register( $c, $action )
343              
344             Calls register_path for every Path attribute for the given $action.
345 1192 100       6604  
346 653         2202 =cut
347              
348             my ( $self, $c, $action ) = @_;
349              
350             my @chained_attr = @{ $action->attributes->{Chained} || [] };
351              
352             return 0 unless @chained_attr;
353              
354             if (@chained_attr > 1) {
355             Catalyst::Exception->throw(
356 61249     61249 1 135699 "Multiple Chained attributes not supported registering ${action}"
357             );
358 61249 100       96129 }
  61249         1587032  
359             my $chained_to = $chained_attr[0];
360 61249 100       239515  
361             Catalyst::Exception->throw(
362 12297 100       28451 "Actions cannot chain to themselves registering /${action}"
363 1         5 ) if ($chained_to eq '/' . $action);
364              
365             my $children = ($self->_children_of->{ $chained_to } ||= {});
366              
367 12296         21823 my @path_part = @{ $action->attributes->{PathPart} || [] };
368              
369 12296 100       48439 my $part = $action->name;
370              
371             if (@path_part == 1 && defined $path_part[0]) {
372             $part = $path_part[0];
373 12294   100     390648 } elsif (@path_part > 1) {
374             Catalyst::Exception->throw(
375 12294 100       22102 "Multiple PathPart attributes not supported registering " . $action->reverse()
  12294         310372  
376             );
377 12294         307337 }
378              
379 12294 100 100     54402 if ($part =~ m(^/)) {
    50          
380 10223         20260 Catalyst::Exception->throw(
381             "Absolute parameters to PathPart not allowed registering " . $action->reverse()
382 0         0 );
383             }
384              
385             my $encoded_part = URI->new($part)->canonical;
386             $encoded_part =~ s{(?<=[^/])/+\z}{};
387 12294 100       29186  
388 1         30 $action->attributes->{PathPart} = [ $encoded_part ];
389              
390             unshift(@{ $children->{$encoded_part} ||= [] }, $action);
391              
392             $self->_actions->{'/'.$action->reverse} = $action;
393 12293         47155  
394 12293         740107 if (exists $action->attributes->{Args} and exists $action->attributes->{CaptureArgs}) {
395             Catalyst::Exception->throw(
396 12293         403619 "Combining Args and CaptureArgs attributes not supported registering " .
397             $action->reverse()
398 12293   100     22001 );
  12293         34707  
399             }
400 12293         465558  
401             unless ($action->attributes->{CaptureArgs}) {
402 12293 50 66     312270 unshift(@{ $self->_endpoints }, $action);
403 0         0 }
404              
405             return 1;
406             }
407              
408             =head2 $self->uri_for_action($action, $captures)
409 12293 100       306335  
410 7101         12283 Get the URI part for the action, using C<$captures> to fill
  7101         213601  
411             the capturing parts.
412              
413 12293         57326 =cut
414              
415             my ( $self, $action, $captures ) = @_;
416              
417             return undef unless ($action->attributes->{Chained}
418             && !$action->attributes->{CaptureArgs});
419              
420             my @parts = ();
421             my @captures = @$captures;
422             my $parent = "DUMMY";
423             my $curr = $action;
424 55     55 1 143 # If this is an action chain get the last action in the chain
425             if($curr->can('chain') ) {
426             $curr = ${$curr->chain}[-1];
427 55 100 66     1520 }
428             while ($curr) {
429 51         131 if (my $cap = $curr->number_of_captures) {
430 51         135 return undef unless @captures >= $cap; # not enough captures
431 51         97 if ($cap) {
432 51         94 unshift(@parts, splice(@captures, -$cap));
433             }
434 51 100       260 }
435 8         16 if (my $pp = $curr->attributes->{PathPart}) {
  8         222  
436             unshift(@parts, $pp->[0])
437 51         176 if (defined($pp->[0]) && length($pp->[0]));
438 124 100       3320 }
439 57 100       185 $parent = $curr->attributes->{Chained}->[0];
440 55 50       137 $curr = $self->_actions->{$parent};
441 55         172 }
442              
443             return undef unless $parent eq '/'; # fail for dangling action
444 122 50       3092  
445 122 100 66     597 return undef if @captures; # fail for too many captures
446              
447             return join('/', '', @parts);
448 122         3766  
449 122         3628 }
450              
451             =head2 $c->expand_action($action)
452 49 50       144  
453             Return a list of actions that represents a chained action. See
454 49 100       127 L<Catalyst::Dispatcher> for more info. You probably want to
455             use the expand_action it provides rather than this directly.
456 47         186  
457             =cut
458              
459             my ($self, $action) = @_;
460              
461             return unless $action->attributes && $action->attributes->{Chained};
462              
463             my @chain;
464             my $curr = $action;
465              
466             while ($curr) {
467             push @chain, $curr;
468             my $parent = $curr->attributes->{Chained}->[0];
469 168     168 1 410 $curr = $self->_actions->{$parent};
470             }
471 168 100 66     4521  
472             return Catalyst::ActionChain->from_chain([reverse @chain]);
473 97         213 }
474 97         171  
475             __PACKAGE__->meta->make_immutable;
476 97         341 1;
477 240         547  
478 240         5952 =head1 USAGE
479 240         7042  
480             =head2 Introduction
481              
482 97         510 The C<Chained> attribute allows you to chain public path parts together
483             by their private names. A chain part's path can be specified with
484             C<PathPart> and can be declared to expect an arbitrary number of
485             arguments. The endpoint of the chain specifies how many arguments it
486             gets through the C<Args> attribute. C<:Args(0)> would be none at all,
487             C<:Args> without an integer would be unlimited. The path parts that
488             aren't endpoints are using C<CaptureArgs> to specify how many parameters
489             they expect to receive. As an example setup:
490              
491             package MyApp::Controller::Greeting;
492             use base qw/ Catalyst::Controller /;
493              
494             # this is the beginning of our chain
495             sub hello : PathPart('hello') Chained('/') CaptureArgs(1) {
496             my ( $self, $c, $integer ) = @_;
497             $c->stash->{ message } = "Hello ";
498             $c->stash->{ arg_sum } = $integer;
499             }
500              
501             # this is our endpoint, because it has no :CaptureArgs
502             sub world : PathPart('world') Chained('hello') Args(1) {
503             my ( $self, $c, $integer ) = @_;
504             $c->stash->{ message } .= "World!";
505             $c->stash->{ arg_sum } += $integer;
506              
507             $c->response->body( join "<br/>\n" =>
508             $c->stash->{ message }, $c->stash->{ arg_sum } );
509             }
510              
511             The debug output provides a separate table for chained actions, showing
512             the whole chain as it would match and the actions it contains. Here's an
513             example of the startup output with our actions above:
514              
515             ...
516             [debug] Loaded Path Part actions:
517             .-----------------------+------------------------------.
518             | Path Spec | Private |
519             +-----------------------+------------------------------+
520             | /hello/*/world/* | /greeting/hello (1) |
521             | | => /greeting/world |
522             '-----------------------+------------------------------'
523             ...
524              
525             As you can see, Catalyst only deals with chains as whole paths and
526             builds one for each endpoint, which are the actions with C<:Chained> but
527             without C<:CaptureArgs>.
528              
529             Let's assume this application gets a request at the path
530             C</hello/23/world/12>. What happens then? First, Catalyst will dispatch
531             to the C<hello> action and pass the value C<23> as an argument to it
532             after the context. It does so because we have previously used
533             C<:CaptureArgs(1)> to declare that it has one path part after itself as
534             its argument. We told Catalyst that this is the beginning of the chain
535             by specifying C<:Chained('/')>. Also note that instead of saying
536             C<:PathPart('hello')> we could also just have said C<:PathPart>, as it
537             defaults to the name of the action.
538              
539             After C<hello> has run, Catalyst goes on to dispatch to the C<world>
540             action. This is the last action to be called: Catalyst knows this is an
541             endpoint because we did not specify a C<:CaptureArgs>
542             attribute. Nevertheless we specify that this action expects an argument,
543             but at this point we're using C<:Args(1)> to do that. We could also have
544             said C<:Args> or left it out altogether, which would mean this action
545             would get all arguments that are there. This action's C<:Chained>
546             attribute says C<hello> and tells Catalyst that the C<hello> action in
547             the current controller is its parent.
548              
549             With this we have built a chain consisting of two public path parts.
550             C<hello> captures one part of the path as its argument, and also
551             specifies the path root as its parent. So this part is
552             C</hello/$arg>. The next part is the endpoint C<world>, expecting one
553             argument. It sums up to the path part C<world/$arg>. This leads to a
554             complete chain of C</hello/$arg/world/$arg> which is matched against the
555             requested paths.
556              
557             This example application would, if run and called by e.g.
558             C</hello/23/world/12>, set the stash value C<message> to "Hello" and the
559             value C<arg_sum> to "23". The C<world> action would then append "World!"
560             to C<message> and add C<12> to the stash's C<arg_sum> value. For the
561             sake of simplicity no view is shown. Instead we just put the values of
562             the stash into our body. So the output would look like:
563              
564             Hello World!
565             35
566              
567             And our test server would have given us this debugging output for the
568             request:
569              
570             ...
571             [debug] "GET" request for "hello/23/world/12" from "127.0.0.1"
572             [debug] Path is "/greeting/world"
573             [debug] Arguments are "12"
574             [info] Request took 0.164113s (6.093/s)
575             .------------------------------------------+-----------.
576             | Action | Time |
577             +------------------------------------------+-----------+
578             | /greeting/hello | 0.000029s |
579             | /greeting/world | 0.000024s |
580             '------------------------------------------+-----------'
581             ...
582              
583             What would be common uses of this dispatch technique? It gives the
584             possibility to split up logic that contains steps that each depend on
585             each other. An example would be, for example, a wiki path like
586             C</wiki/FooBarPage/rev/23/view>. This chain can be easily built with
587             these actions:
588              
589             sub wiki : PathPart('wiki') Chained('/') CaptureArgs(1) {
590             my ( $self, $c, $page_name ) = @_;
591             # load the page named $page_name and put the object
592             # into the stash
593             }
594              
595             sub rev : PathPart('rev') Chained('wiki') CaptureArgs(1) {
596             my ( $self, $c, $revision_id ) = @_;
597             # use the page object in the stash to get at its
598             # revision with number $revision_id
599             }
600              
601             sub view : PathPart Chained('rev') Args(0) {
602             my ( $self, $c ) = @_;
603             # display the revision in our stash. Another option
604             # would be to forward a compatible object to the action
605             # that displays the default wiki pages, unless we want
606             # a different interface here, for example restore
607             # functionality.
608             }
609              
610             It would now be possible to add other endpoints, for example C<restore>
611             to restore this specific revision as the current state.
612              
613             You don't have to put all the chained actions in one controller. The
614             specification of the parent through C<:Chained> also takes an absolute
615             action path as its argument. Just specify it with a leading C</>.
616              
617             If you want, for example, to have actions for the public paths
618             C</foo/12/edit> and C</foo/12>, just specify two actions with
619             C<:PathPart('foo')> and C<:Chained('/')>. The handler for the former
620             path needs a C<:CaptureArgs(1)> attribute and a endpoint with
621             C<:PathPart('edit')> and C<:Chained('foo')>. For the latter path give
622             the action just a C<:Args(1)> to mark it as endpoint. This sums up to
623             this debugging output:
624              
625             ...
626             [debug] Loaded Path Part actions:
627             .-----------------------+------------------------------.
628             | Path Spec | Private |
629             +-----------------------+------------------------------+
630             | /foo/* | /controller/foo_view |
631             | /foo/*/edit | /controller/foo_load (1) |
632             | | => /controller/edit |
633             '-----------------------+------------------------------'
634             ...
635              
636             Here's a more detailed specification of the attributes belonging to
637             C<:Chained>:
638              
639             =head2 Attributes
640              
641             =over 8
642              
643             =item PathPart
644              
645             Sets the name of this part of the chain. If it is specified without
646             arguments, it takes the name of the action as default. So basically
647             C<sub foo :PathPart> and C<sub foo :PathPart('foo')> are identical.
648             This can also contain slashes to bind to a deeper level. An action
649             with C<sub bar :PathPart('foo/bar') :Chained('/')> would bind to
650             C</foo/bar/...>. If you don't specify C<:PathPart> it has the same
651             effect as using C<:PathPart>, it would default to the action name.
652              
653             =item PathPrefix
654              
655             Sets PathPart to the path_prefix of the current controller.
656              
657             =item Chained
658              
659             Has to be specified for every child in the chain. Possible values are
660             absolute and relative private action paths or a single slash C</> to
661             tell Catalyst that this is the root of a chain. The attribute
662             C<:Chained> without arguments also defaults to the C</> behavior.
663             Relative action paths may use C<../> to refer to actions in parent
664             controllers.
665              
666             Because you can specify an absolute path to the parent action, it
667             doesn't matter to Catalyst where that parent is located. So, if your
668             design requests it, you can redispatch a chain through any controller or
669             namespace you want.
670              
671             Another interesting possibility gives C<:Chained('.')>, which chains
672             itself to an action with the path of the current controller's namespace.
673             For example:
674              
675             # in MyApp::Controller::Foo
676             sub bar : Chained CaptureArgs(1) { ... }
677              
678             # in MyApp::Controller::Foo::Bar
679             sub baz : Chained('.') Args(1) { ... }
680              
681             This builds up a chain like C</bar/*/baz/*>. The specification of C<.>
682             as the argument to Chained here chains the C<baz> action to an action
683             with the path of the current controller namespace, namely
684             C</foo/bar>. That action chains directly to C</>, so the C</bar/*/baz/*>
685             chain comes out as the end product.
686              
687             =item ChainedParent
688              
689             Chains an action to another action with the same name in the parent
690             controller. For Example:
691              
692             # in MyApp::Controller::Foo
693             sub bar : Chained CaptureArgs(1) { ... }
694              
695             # in MyApp::Controller::Foo::Bar
696             sub bar : ChainedParent Args(1) { ... }
697              
698             This builds a chain like C</bar/*/bar/*>.
699              
700             =item CaptureArgs
701              
702             Must be specified for every part of the chain that is not an
703             endpoint. With this attribute Catalyst knows how many of the following
704             parts of the path (separated by C</>) this action wants to capture as
705             its arguments. If it doesn't expect any, just specify
706             C<:CaptureArgs(0)>. The captures get passed to the action's C<@_> right
707             after the context, but you can also find them as array references in
708             C<< $c->request->captures->[$level] >>. The C<$level> is the
709             level of the action in the chain that captured the parts of the path.
710              
711             An action that is part of a chain (that is, one that has a C<:Chained>
712             attribute) but has no C<:CaptureArgs> attribute is treated by Catalyst
713             as a chain end.
714              
715             Allowed values for CaptureArgs is a single integer (CaptureArgs(2), meaning two
716             allowed) or you can declare a L<Moose>, L<MooseX::Types> or L<Type::Tiny>
717             named constraint such as CaptureArgs(Int,Str) would require two args with
718             the first being a Integer and the second a string. You may declare your own
719             custom type constraints and import them into the controller namespace:
720              
721             package MyApp::Controller::Root;
722              
723             use Moose;
724             use MooseX::MethodAttributes;
725             use MyApp::Types qw/Int/;
726              
727             extends 'Catalyst::Controller';
728              
729             sub chain_base :Chained(/) CaptureArgs(1) { }
730              
731             sub any_priority_chain :Chained(chain_base) PathPart('') Args(1) { }
732              
733             sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { }
734              
735             If you use a reference type constraint in CaptureArgs, it must be a type
736             like Tuple in L<Types::Standard> that allows us to determine the number of
737             args to match. Otherwise this will raise an error during startup.
738              
739             See L<Catalyst::RouteMatching> for more.
740              
741             =item Args
742              
743             By default, endpoints receive the rest of the arguments in the path. You
744             can tell Catalyst through C<:Args> explicitly how many arguments your
745             endpoint expects, just like you can with C<:CaptureArgs>. Note that this
746             also affects whether this chain is invoked on a request. A chain with an
747             endpoint specifying one argument will only match if exactly one argument
748             exists in the path.
749              
750             You can specify an exact number of arguments like C<:Args(3)>, including
751             C<0>. If you just say C<:Args> without any arguments, it is the same as
752             leaving it out altogether: The chain is matched regardless of the number
753             of path parts after the endpoint.
754              
755             Just as with C<:CaptureArgs>, the arguments get passed to the action in
756             C<@_> after the context object. They can also be reached through
757             C<< $c->request->arguments >>.
758              
759             You should see 'Args' in L<Catalyst::Controller> for more details on using
760             type constraints in your Args declarations.
761              
762             =back
763              
764             =head2 Auto actions, dispatching and forwarding
765              
766             Note that the list of C<auto> actions called depends on the private path
767             of the endpoint of the chain, not on the chained actions way. The
768             C<auto> actions will be run before the chain dispatching begins. In
769             every other aspect, C<auto> actions behave as documented.
770              
771             The C<forward>ing to other actions does just what you would expect. i.e.
772             only the target action is run. The actions that that action is chained
773             to are not run.
774             If you C<detach> out of a chain, the rest of the chain will not get
775             called after the C<detach>.
776              
777             =head2 match_captures
778              
779             A method which can optionally be implemented by actions to
780             stop chain matching.
781              
782             See L<Catalyst::Action> for further details.
783              
784             =head1 AUTHORS
785              
786             Catalyst Contributors, see Catalyst.pm
787              
788             =head1 COPYRIGHT
789              
790             This library is free software. You can redistribute it and/or modify it under
791             the same terms as Perl itself.
792              
793             =cut
794              
795             1;