File Coverage

blib/lib/Catalyst/Controller.pm
Criterion Covered Total %
statement 253 261 96.9
branch 90 100 90.0
condition 21 30 70.0
subroutine 53 62 85.4
pod 8 9 88.8
total 425 462 91.9


line stmt bran cond sub pod time code
1             package Catalyst::Controller;
2              
3 167     167   7435463 use Moose;
  167         11454809  
  167         1522  
4 167     167   1220234 use Class::MOP;
  167         960  
  167         5771  
5 167     167   1193 use Class::Load ':all';
  167         511  
  167         30932  
6 167     167   20685 use String::RewritePrefix;
  167         37998  
  167         2035  
7 167     167   36798 use Moose::Util qw/find_meta/;
  167         533  
  167         1828  
8 167     167   42475 use List::Util qw/first uniq/;
  167         488  
  167         14912  
9 167     167   14525 use namespace::clean -except => 'meta';
  167         178660  
  167         2039  
10              
11             BEGIN {
12 167     167   84141 extends qw/Catalyst::Component/;
13 167         205998 with qw/MooseX::MethodAttributes::Role::AttrContainer::Inheritable/;
14             }
15              
16 167     167   9790000 use MooseX::MethodAttributes;
  167         537  
  167         1188  
17 167     167   14067335 use Catalyst::Exception;
  167         595  
  167         5554  
18 167     167   1255 use Catalyst::Utils;
  167         467  
  167         95744  
19              
20             with 'Catalyst::Component::ApplicationAttribute';
21              
22             has path_prefix => (
23             is => 'rw',
24             isa => 'Str',
25             init_arg => 'path',
26             predicate => 'has_path_prefix',
27             );
28              
29             has action_namespace => (
30             is => 'rw',
31             isa => 'Str',
32             init_arg => 'namespace',
33             predicate => 'has_action_namespace',
34             );
35              
36             has actions => (
37             accessor => '_controller_actions',
38             isa => 'HashRef',
39             init_arg => undef,
40             );
41              
42             has _action_role_args => (
43             traits => [qw(Array)],
44             isa => 'ArrayRef[Str]',
45             init_arg => 'action_roles',
46             default => sub { [] },
47             handles => {
48             _action_role_args => 'elements',
49             },
50             );
51              
52             has _action_roles => (
53             traits => [qw(Array)],
54             isa => 'ArrayRef[RoleName]',
55             init_arg => undef,
56             lazy => 1,
57             builder => '_build__action_roles',
58             handles => {
59             _action_roles => 'elements',
60             },
61             );
62              
63             has action_args => (is => 'ro');
64              
65             # ->config(actions => { '*' => ...
66             has _all_actions_attributes => (
67             is => 'ro',
68             isa => 'HashRef',
69             init_arg => undef,
70             lazy => 1,
71             builder => '_build__all_actions_attributes',
72             );
73              
74             sub BUILD {
75 7086     7086 0 10960347 my ($self, $args) = @_;
76 7086   100     36865 my $action = delete $args->{action} || {};
77 7086   100     25402 my $actions = delete $args->{actions} || {};
78 7086         24753 my $attr_value = $self->merge_config_hashes($actions, $action);
79 7086         324151 $self->_controller_actions($attr_value);
80              
81             # trigger lazy builder
82 7086         247134 $self->_all_actions_attributes;
83 7086         284445 $self->_action_roles;
84             }
85              
86             sub _build__action_roles {
87 7086     7086   11810 my $self = shift;
88 7086         290871 my @roles = $self->_expand_role_shortname($self->_action_role_args);
89 7086         411043 load_class($_) for @roles;
90 7086         970194 return \@roles;
91             }
92              
93             sub _build__all_actions_attributes {
94 7086     7086   14428 my ($self) = @_;
95 7086 100       231921 delete $self->_controller_actions->{'*'} || {};
96             }
97              
98             =head1 NAME
99              
100             Catalyst::Controller - Catalyst Controller base class
101              
102             =head1 SYNOPSIS
103              
104             package MyApp::Controller::Search
105             use base qw/Catalyst::Controller/;
106              
107             sub foo : Local {
108             my ($self,$c,@args) = @_;
109             ...
110             } # Dispatches to /search/foo
111              
112             =head1 DESCRIPTION
113              
114             Controllers are where the actions in the Catalyst framework
115             reside. Each action is represented by a function with an attribute to
116             identify what kind of action it is. See the L<Catalyst::Dispatcher>
117             for more info about how Catalyst dispatches to actions.
118              
119             =cut
120              
121             #I think both of these could be attributes. doesn't really seem like they need
122             #to be class data. i think that attributes +default would work just fine
123             __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class _action_role_prefix/;
124              
125             __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
126             __PACKAGE__->_action_class('Catalyst::Action');
127             __PACKAGE__->_action_role_prefix([ 'Catalyst::ActionRole::' ]);
128              
129              
130             sub _DISPATCH : Private {
131 960     960   2850 my ( $self, $c ) = @_;
132              
133 960         1873 foreach my $disp ( @{ $self->_dispatch_steps } ) {
  960         4904  
134 2874 100       9870 last unless $c->forward($disp);
135             }
136              
137 934         4184 $c->forward('_END');
138 167     167   96545 }
  167         203182  
  167         1381  
139              
140             sub _BEGIN : Private {
141 960     960   3268 my ( $self, $c ) = @_;
142 960         24844 my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
143 960 100       5601 return 1 unless $begin;
144 300         1481 $begin->dispatch( $c );
145             #If there is an error, all bets off
146 300 50       813 if( @{ $c->error }) {
  300         1555  
147 0         0 return !@{ $c->error };
  0         0  
148             } else {
149 300   100     8094 return $c->state || 1;
150             }
151 167     167   242228 }
  167         618  
  167         944  
152              
153             sub _AUTO : Private {
154 960     960   3164 my ( $self, $c ) = @_;
155 960         24689 my @auto = $c->get_actions( 'auto', $c->namespace );
156 960         3353 foreach my $auto (@auto) {
157             # We FORCE the auto action user to explicitly return
158             # true. We need to do this since there's some auto
159             # users (Catalyst::Authentication::Credential::HTTP) that
160             # actually do a detach instead.
161 40         974 $c->state(0);
162 40         173 $auto->dispatch( $c );
163 36 100       845 return 0 unless $c->state;
164             }
165 954   100     24154 return $c->state || 1;
166 167     167   180190 }
  167         680  
  167         980  
167              
168             sub _ACTION : Private {
169 954     954   3338 my ( $self, $c ) = @_;
170 954 50 33     23927 if ( ref $c->action
      33        
171             && $c->action->can('execute')
172             && defined $c->req->action )
173             {
174 954         22950 $c->action->dispatch( $c );
175             }
176             #If there is an error, all bets off
177 923 100       3005 if( @{ $c->error }) {
  923         3688  
178 26         67 return !@{ $c->error };
  26         101  
179             } else {
180 897   100     23137 return $c->state || 1;
181             }
182 167     167   181436 }
  167         741  
  167         1208  
183              
184             sub _END : Private {
185 934     934   3255 my ( $self, $c ) = @_;
186 934         24230 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
187 934 100       4796 return 1 unless $end;
188 646         3257 $end->dispatch( $c );
189 645         1566 return !@{ $c->error };
  645         2949  
190 167     167   178019 }
  167         733  
  167         1058  
191              
192             sub action_for {
193 65     65 1 1330 my ( $self, $name ) = @_;
194 65 100       2679 my $app = ($self->isa('Catalyst') ? $self : $self->_application);
195 65         272 return $app->dispatcher->get_action($name, $self->action_namespace);
196             }
197              
198             #my opinion is that this whole sub really should be a builder method, not
199             #something that happens on every call. Anyone else disagree?? -- groditi
200             ## -- apparently this is all just waiting for app/ctx split
201             around action_namespace => sub {
202             my $orig = shift;
203             my ( $self, $c ) = @_;
204              
205             my $class = ref($self) || $self;
206             my $appclass = ref($c) || $c;
207             if( ref($self) ){
208             return $self->$orig if $self->has_action_namespace;
209             } else {
210             return $class->config->{namespace} if exists $class->config->{namespace};
211             }
212              
213             my $case_s;
214             if( $c ){
215             $case_s = $appclass->config->{case_sensitive};
216             } else {
217             if ($self->isa('Catalyst')) {
218             $case_s = $class->config->{case_sensitive};
219             } else {
220             if (ref $self) {
221             $case_s = ref($self->_application)->config->{case_sensitive};
222             } else {
223             confess("Can't figure out case_sensitive setting");
224             }
225             }
226             }
227              
228             my $namespace = Catalyst::Utils::class2prefix($self->catalyst_component_name, $case_s) || '';
229             $self->$orig($namespace) if ref($self);
230             return $namespace;
231             };
232              
233             #Once again, this is probably better written as a builder method
234             around path_prefix => sub {
235             my $orig = shift;
236             my $self = shift;
237             if( ref($self) ){
238             return $self->$orig if $self->has_path_prefix;
239             } else {
240             return $self->config->{path} if exists $self->config->{path};
241             }
242             my $namespace = $self->action_namespace(@_);
243             $self->$orig($namespace) if ref($self);
244             return $namespace;
245             };
246              
247             sub get_action_methods {
248 6521     6521 1 15166 my $self = shift;
249 6521   33     21765 my $meta = find_meta($self) || confess("No metaclass setup for $self");
250 6521 50       126005 confess(
251             sprintf "Metaclass %s for %s cannot support register_actions.",
252             ref $meta, $meta->name,
253             ) unless $meta->can('get_nearest_methods_with_attributes');
254 6521         25039 my @methods = $meta->get_nearest_methods_with_attributes;
255              
256             # actions specified via config are also action_methods
257             push(
258             @methods,
259             map {
260 655 50       15944 $meta->find_method_by_name($_)
261             || confess( sprintf 'Action "%s" is not available from controller %s',
262             $_, ref $self )
263 6521 100       62133641 } keys %{ $self->_controller_actions }
  6371         222517  
264             ) if ( ref $self );
265 6521         67536 return uniq @methods;
266             }
267              
268              
269             sub register_actions {
270 6519     6519 1 18129 my ( $self, $c ) = @_;
271 6519         40816 $self->register_action_methods( $c, $self->get_action_methods );
272             }
273              
274             sub register_action_methods {
275 6519     6519 1 458999 my ( $self, $c, @methods ) = @_;
276 6519         48166 my $class = $self->catalyst_component_name;
277             #this is still not correct for some reason.
278 6519         49817 my $namespace = $self->action_namespace($c);
279              
280             # FIXME - fugly
281 6519 50 66     33865 if (!blessed($self) && $self eq $c && scalar(@methods)) {
      100        
282 149         712 my @really_bad_methods = grep { ! /^_(DISPATCH|BEGIN|AUTO|ACTION|END)$/ } map { $_->name } @methods;
  747         3488  
  747         2533  
283 149 100       1004 if (scalar(@really_bad_methods)) {
284 2         18 $c->log->warn("Action methods (" . join(', ', @really_bad_methods) . ") found defined in your application class, $self. This is deprecated, please move them into a Root controller.");
285             }
286             }
287              
288 6519         19507 foreach my $method (@methods) {
289 69208         288673 my $name = $method->name;
290             # Horrible hack! All method metaclasses should have an attributes
291             # method, core Moose bug - see r13354.
292 69208 100       2376663 my $attributes = $method->can('attributes') ? $method->attributes : [];
293 69208         739585 my $attrs = $self->_parse_attrs( $c, $name, @{ $attributes } );
  69208         262704  
294 69208 50 66     270450 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
295             $c->log->warn( 'Bad action definition "'
296 0 0       0 . join( ' ', @{ $attributes } )
  0         0  
297             . qq/" for "$class->$name"/ )
298             if $c->debug;
299 0         0 next;
300             }
301 69208 100       215711 my $reverse = $namespace ? "${namespace}/${name}" : $name;
302 69208         308560 my $action = $self->create_action(
303             name => $name,
304             code => $method->body,
305             reverse => $reverse,
306             namespace => $namespace,
307             class => $class,
308             attributes => $attrs,
309             );
310              
311 69208         6145809 $c->dispatcher->register( $c, $action );
312             }
313             }
314              
315             sub _apply_action_class_roles {
316 1818     1818   5167 my ($self, $class, @roles) = @_;
317              
318 1818         6718 load_class($_) for @roles;
319 1818         661364 my $meta = Moose::Meta::Class->initialize($class)->create_anon_class(
320             superclasses => [$class],
321             roles => \@roles,
322             cache => 1,
323             );
324 1818     0   4421900 $meta->add_method(meta => sub { $meta });
  0     0   0  
        0      
        0      
        0      
        0      
        0      
325              
326 1818         103591 return $meta->name;
327             }
328              
329             sub action_class {
330 69208     69208 1 107376 my $self = shift;
331 69208         209665 my %args = @_;
332              
333             my $class = (exists $args{attributes}{ActionClass}
334 69208 100       286570 ? $args{attributes}{ActionClass}[0]
335             : $self->_action_class);
336              
337 69208         265710 load_class($class);
338 69208         8201444 return $class;
339             }
340              
341             sub create_action {
342 69208     69208 1 115040 my $self = shift;
343 69208         327939 my %args = @_;
344              
345 69208         232606 my $class = $self->action_class(%args);
346              
347 69208         206325 load_class($class);
348 69208 100       1770569 Moose->init_meta(for_class => $class)
349             unless Class::MOP::does_metaclass_exist($class);
350              
351 69208 100       1869876 unless ($args{name} =~ /^_(DISPATCH|BEGIN|AUTO|ACTION|END)$/) {
352 36613         148828 my @roles = $self->gather_action_roles(%args);
353 36613         154989 push @roles, $self->gather_default_action_roles(%args);
354              
355 36613 100       98542 $class = $self->_apply_action_class_roles($class, @roles) if @roles;
356             }
357              
358             my $action_args = (
359             ref($self)
360             ? $self->action_args
361             : $self->config->{action_args}
362 69208 100       2226648 );
363              
364             my %extra_args = (
365 69208 100       308052 %{ $action_args->{'*'} || {} },
366 69208 100       113581 %{ $action_args->{ $args{name} } || {} },
  69208         291315  
367             );
368              
369 69208         2211891 return $class->new({ %extra_args, %args });
370             }
371              
372             sub gather_action_roles {
373 36613     36613 1 143093 my ($self, %args) = @_;
374             return (
375             (blessed $self ? $self->_action_roles : ()),
376 36613 100       1644922 @{ $args{attributes}->{Does} || [] },
  36613 100       221398  
377             );
378             }
379              
380             sub gather_default_action_roles {
381 36613     36613 1 127556 my ($self, %args) = @_;
382 36613         63739 my @roles = ();
383             push @roles, 'Catalyst::ActionRole::HTTPMethods'
384 36613 100       87774 if $args{attributes}->{Method};
385              
386             push @roles, 'Catalyst::ActionRole::ConsumesContent'
387 36613 100       77210 if $args{attributes}->{Consumes};
388              
389             push @roles, 'Catalyst::ActionRole::Scheme'
390 36613 100       72426 if $args{attributes}->{Scheme};
391              
392             push @roles, 'Catalyst::ActionRole::QueryMatching'
393 36613 100       72478 if $args{attributes}->{Query};
394 36613         93420 return @roles;
395             }
396              
397             sub _parse_attrs {
398 69208     69208   227588 my ( $self, $c, $name, @attrs ) = @_;
399              
400 69208         110366 my %raw_attributes;
401              
402 69208         133579 foreach my $attr (@attrs) {
403              
404             # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
405              
406 96151 50       675378 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)?\s*\))?$/ ) )
407             {
408              
409 96151 100       216252 if ( defined $value ) {
410 35983 100       145959 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
411             }
412 96151         142981 push( @{ $raw_attributes{$key} }, $value );
  96151         351550  
413             }
414             }
415              
416 69208         122063 my ($actions_config, $all_actions_config);
417 69208 100       159511 if( ref($self) ) {
418 68461         2345815 $actions_config = $self->_controller_actions;
419             # No, you're not getting actions => { '*' => ... } with actions in MyApp.
420 68461         2004597 $all_actions_config = $self->_all_actions_attributes;
421             } else {
422 747         2823 my $cfg = $self->config;
423 747         4792 $actions_config = $self->merge_config_hashes($cfg->{actions}, $cfg->{action});
424 747         2377 $all_actions_config = {};
425             }
426              
427             %raw_attributes = (
428             %raw_attributes,
429             # Note we deep copy array refs here to stop crapping on config
430             # when attributes are parsed. RT#65463
431 69208 100       313963 exists $actions_config->{$name} ? map { ref($_) eq 'ARRAY' ? [ @$_ ] : $_ } %{ $actions_config->{$name } } : (),
  1146 100       6176  
  654         4484  
432             );
433              
434             # Private actions with additional attributes will raise a warning and then
435             # be ignored. Adding '*' arguments to the default _DISPATCH / etc. methods,
436             # which are Private, will prevent those from being registered. They should
437             # probably be turned into :Actions instead, or we might want to otherwise
438             # disambiguate between those built-in internal actions and user-level
439             # Private ones.
440 27606         86005 %raw_attributes = (%{ $all_actions_config }, %raw_attributes)
441 69208 100       177521 unless $raw_attributes{Private};
442              
443 69208         109833 my %final_attributes;
444              
445 69208         210691 while (my ($key, $value) = each %raw_attributes){
446 97235         258265 my $new_attrs = $self->_parse_attr($c, $name, $key => $value );
447 97235         253734 push @{ $final_attributes{$_} }, @{ $new_attrs->{$_} } for keys %$new_attrs;
  97478         205141  
  97478         466564  
448             }
449              
450 69208         211409 return \%final_attributes;
451             }
452              
453             sub _parse_attr {
454 112654     112654   239695 my ($self, $c, $name, $key, $values) = @_;
455              
456 112654         158046 my %final_attributes;
457 112654 100       305821 foreach my $value (ref($values) eq 'ARRAY' ? @$values : $values) {
458 112818         243919 my $meth = "_parse_${key}_attr";
459 112818 100       450290 if ( my $code = $self->can($meth) ) {
460 44337         110435 my %new_attrs = $self->$code( $c, $name, $value );
461 44337         170724 while (my ($new_key, $value) = each %new_attrs){
462 44580 100       151797 my $new_attrs = $key eq $new_key ?
463             { $new_key => [$value] } :
464             $self->_parse_attr($c, $name, $new_key => $value );
465 44580         126365 push @{ $final_attributes{$_} }, @{ $new_attrs->{$_} } for keys %$new_attrs;
  44580         105835  
  44580         255699  
466             }
467             }
468             else {
469 68481         101833 push( @{ $final_attributes{$key} }, $value );
  68481         233296  
470             }
471             }
472              
473 112654         255167 return \%final_attributes;
474             }
475              
476             sub _parse_Global_attr {
477 1728     1728   4990 my ( $self, $c, $name, $value ) = @_;
478             # _parse_attr will call _parse_Path_attr for us
479 1728         7102 return Path => "/$name";
480             }
481              
482 81     81   856 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
483              
484             sub _parse_Local_attr {
485 11482     11482   29313 my ( $self, $c, $name, $value ) = @_;
486             # _parse_attr will call _parse_Path_attr for us
487 11482         36784 return Path => $name;
488             }
489              
490 2025     2025   7462 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
491              
492             sub _parse_Path_attr {
493 15366     15366   34026 my ( $self, $c, $name, $value ) = @_;
494 15366 100       34527 $value = '' if !defined $value;
495 15366 100       51003 if ( $value =~ m!^/! ) {
    100          
496 2140         7295 return ( 'Path', $value );
497             }
498             elsif ( length $value ) {
499 12633         53858 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
500             }
501             else {
502 593         4111 return ( 'Path', $self->path_prefix($c) );
503             }
504             }
505              
506             sub _parse_Chained_attr {
507 12391     12391   30391 my ($self, $c, $name, $value) = @_;
508              
509 12391 100 66     48895 if (defined($value) && length($value)) {
510 11481 100       58076 if ($value eq '.') {
    100          
    100          
511 324         1725 $value = '/'.$self->action_namespace($c);
512             } elsif (my ($rel, $rest) = $value =~ /^((?:\.{2}\/)+)(.*)$/) {
513 324         1486 my @parts = split '/', $self->action_namespace($c);
514 324         1105 my @levels = split '/', $rel;
515              
516 324         2356 $value = '/'.join('/', @parts[0 .. $#parts - @levels], $rest);
517             } elsif ($value !~ m/^\//) {
518 6564         25110 my $action_ns = $self->action_namespace($c);
519              
520 6564 100       15970 if ($action_ns) {
521 6414         23640 $value = '/'.join('/', $action_ns, $value);
522             } else {
523 150         731 $value = '/'.$value; # special case namespace '' (root)
524             }
525             }
526             } else {
527 910         1984 $value = '/'
528             }
529              
530 12391         40468 return Chained => $value;
531             }
532              
533             sub _parse_ChainedParent_attr {
534 81     81   543 my ($self, $c, $name, $value) = @_;
535 81         510 return $self->_parse_Chained_attr($c, $name, '../'.$name);
536             }
537              
538             sub _parse_PathPrefix_attr {
539 331     331   1656 my ( $self, $c ) = @_;
540 331         2676 return PathPart => $self->path_prefix($c);
541             }
542              
543             sub _parse_ActionClass_attr {
544 918     918   2900 my ( $self, $c, $name, $value ) = @_;
545 918         33571 my $appname = $self->_application;
546 918         4346 $value = Catalyst::Utils::resolve_namespace($appname . '::Action', $self->_action_class, $value);
547 918         74549 return ( 'ActionClass', $value );
548             }
549              
550             sub _parse_MyAction_attr {
551 84     84   421 my ( $self, $c, $name, $value ) = @_;
552              
553 84         596 my $appclass = Catalyst::Utils::class2appclass($self);
554 84         457 $value = "+${appclass}::Action::${value}";
555              
556 84         387 return ( 'ActionClass', $value );
557             }
558              
559             sub _parse_Does_attr {
560 486     486   1550 my ($self, $app, $name, $value) = @_;
561 486         1775 return Does => $self->_expand_role_shortname($value);
562             }
563              
564 330     330   1276 sub _parse_GET_attr { Method => 'GET' }
565 329     329   1274 sub _parse_POST_attr { Method => 'POST' }
566 244     244   941 sub _parse_PUT_attr { Method => 'PUT' }
567 243     243   925 sub _parse_DELETE_attr { Method => 'DELETE' }
568 81     81   430 sub _parse_OPTIONS_attr { Method => 'OPTIONS' }
569 0     0   0 sub _parse_HEAD_attr { Method => 'HEAD' }
570 0     0   0 sub _parse_PATCH_attr { Method => 'PATCH' }
571              
572             sub _expand_role_shortname {
573 7572     7572   18470 my ($self, @shortnames) = @_;
574 7572         234572 my $app = $self->_application;
575              
576 7572 50       53462 my $prefix = $self->can('_action_role_prefix') ? $self->_action_role_prefix : ['Catalyst::ActionRole::'];
577 7572         28489 my @prefixes = (qq{${app}::ActionRole::}, @$prefix);
578              
579             return String::RewritePrefix->rewrite(
580             { '' => sub {
581             my $loaded = load_first_existing_class(
582 324     324   21016 map { "$_$_[0]" } @prefixes
  648         3174  
583             );
584 567         9724 return first { $loaded =~ /^$_/ }
585 324         1781011 sort { length $b <=> length $a } @prefixes;
  324         2296  
586             },
587 7572         85305 '~' => $prefixes[0],
588             '+' => '' },
589             @shortnames,
590             );
591             }
592              
593             __PACKAGE__->meta->make_immutable;
594              
595             1;
596              
597             __END__
598              
599             =head1 CONFIGURATION
600              
601             Like any other L<Catalyst::Component>, controllers have a config hash,
602             accessible through $self->config from the controller actions. Some
603             settings are in use by the Catalyst framework:
604              
605             =head2 namespace
606              
607             This specifies the internal namespace the controller should be bound
608             to. By default the controller is bound to the URI version of the
609             controller name. For instance controller 'MyApp::Controller::Foo::Bar'
610             will be bound to 'foo/bar'. The default Root controller is an example
611             of setting namespace to '' (the null string).
612              
613             =head2 path
614              
615             Sets 'path_prefix', as described below.
616              
617             =head2 action
618              
619             Allows you to set the attributes that the dispatcher creates actions out of.
620             This allows you to do 'rails style routes', or override some of the
621             attribute definitions of actions composed from Roles.
622             You can set arguments globally (for all actions of the controller) and
623             specifically (for a single action).
624              
625             __PACKAGE__->config(
626             action => {
627             '*' => { Chained => 'base', Args => 0 },
628             base => { Chained => '/', PathPart => '', CaptureArgs => 0 },
629             },
630             );
631              
632             In the case above every sub in the package would be made into a Chain
633             endpoint with a URI the same as the sub name for each sub, chained
634             to the sub named C<base>. Ergo dispatch to C</example> would call the
635             C<base> method, then the C<example> method.
636              
637             =head2 action_args
638              
639             Allows you to set constructor arguments on your actions. You can set arguments
640             globally and specifically (as above).
641             This is particularly useful when using C<ActionRole>s
642             (L<Catalyst::Controller::ActionRole>) and custom C<ActionClass>es.
643              
644             __PACKAGE__->config(
645             action_args => {
646             '*' => { globalarg1 => 'hello', globalarg2 => 'goodbye' },
647             'specific_action' => { customarg => 'arg1' },
648             },
649             );
650              
651             In the case above the action class associated with C<specific_action> would get
652             passed the following arguments, in addition to the normal action constructor
653             arguments, when it is instantiated:
654              
655             (globalarg1 => 'hello', globalarg2 => 'goodbye', customarg => 'arg1')
656              
657             =head1 METHODS
658              
659             =head2 BUILDARGS ($app, @args)
660              
661             From L<Catalyst::Component::ApplicationAttribute>, stashes the application
662             instance as $self->_application.
663              
664             =head2 $self->action_for($action_name)
665              
666             Returns the Catalyst::Action object (if any) for a given action in this
667             controller or relative to it. You may refer to actions in controllers
668             nested under the current controllers namespace, or in controllers 'up'
669             from the current controller namespace. For example:
670              
671             package MyApp::Controller::One::Two;
672             use base 'Catalyst::Controller';
673              
674             sub foo :Local {
675             my ($self, $c) = @_;
676             $self->action_for('foo'); # action 'foo' in Controller 'One::Two'
677             $self->action_for('three/bar'); # action 'bar' in Controller 'One::Two::Three'
678             $self->action_for('../boo'); # action 'boo' in Controller 'One'
679             }
680              
681             This returns 'undef' if there is no action matching the requested action
682             name (after any path normalization) so you should check for this as needed.
683              
684             =head2 $self->action_namespace($c)
685              
686             Returns the private namespace for actions in this component. Defaults
687             to a value from the controller name (for
688             e.g. MyApp::Controller::Foo::Bar becomes "foo/bar") or can be
689             overridden from the "namespace" config key.
690              
691              
692             =head2 $self->path_prefix($c)
693              
694             Returns the default path prefix for :PathPrefix, :Local and
695             relative :Path actions in this component. Defaults to the action_namespace or
696             can be overridden from the "path" config key.
697              
698             =head2 $self->register_actions($c)
699              
700             Finds all applicable actions for this component, creates
701             Catalyst::Action objects (using $self->create_action) for them and
702             registers them with $c->dispatcher.
703              
704             =head2 $self->get_action_methods()
705              
706             Returns a list of L<Moose::Meta::Method> objects, doing the
707             L<MooseX::MethodAttributes::Role::Meta::Method> role, which are the set of
708             action methods for this package.
709              
710             =head2 $self->register_action_methods($c, @methods)
711              
712             Creates action objects for a set of action methods using C< create_action >,
713             and registers them with the dispatcher.
714              
715             =head2 $self->action_class(%args)
716              
717             Used when a controller is creating an action to determine the correct base
718             action class to use.
719              
720             =head2 $self->create_action(%args)
721              
722             Called with a hash of data to be use for construction of a new
723             Catalyst::Action (or appropriate sub/alternative class) object.
724              
725             =head2 $self->gather_action_roles(\%action_args)
726              
727             Gathers the list of roles to apply to an action with the given %action_args.
728              
729             =head2 $self->gather_default_action_roles(\%action_args)
730              
731             returns a list of action roles to be applied based on core, builtin rules.
732             Currently only the L<Catalyst::ActionRole::HTTPMethods> role is applied
733             this way.
734              
735             =head2 $self->_application
736              
737             =head2 $self->_app
738              
739             Returns the application instance stored by C<new()>
740              
741             =head1 ACTION SUBROUTINE ATTRIBUTES
742              
743             Please see L<Catalyst::Manual::Intro> for more details
744              
745             Think of action attributes as a sort of way to record metadata about an action,
746             similar to how annotations work in other languages you might have heard of.
747             Generally L<Catalyst> uses these to influence how the dispatcher sees your
748             action and when it will run it in response to an incoming request. They can
749             also be used for other things. Here's a summary, but you should refer to the
750             linked manual page for additional help.
751              
752             =head2 Global
753              
754             sub homepage :Global { ... }
755              
756             A global action defined in any controller always runs relative to your root.
757             So the above is the same as:
758              
759             sub myaction :Path("/homepage") { ... }
760              
761             =head2 Absolute
762              
763             Status: Deprecated alias to L</Global>.
764              
765             =head2 Local
766              
767             Alias to "Path("$action_name"). The following two actions are the same:
768              
769             sub myaction :Local { ... }
770             sub myaction :Path('myaction') { ... }
771              
772             =head2 Relative
773              
774             Status: Deprecated alias to L</Local>
775              
776             =head2 Path
777              
778             Handle various types of paths:
779              
780             package MyApp::Controller::Baz {
781              
782             ...
783              
784             sub myaction1 :Path { ... } # -> /baz
785             sub myaction2 :Path('foo') { ... } # -> /baz/foo
786             sub myaction2 :Path('/bar') { ... } # -> /bar
787             }
788              
789             This is a general toolbox for attaching your action to a given path.
790              
791              
792             =head2 Regex
793              
794             =head2 Regexp
795              
796             B<Status: Deprecated.> Use Chained methods or other techniques.
797             If you really depend on this, install the standalone
798             L<Catalyst::DispatchType::Regex> distribution.
799              
800             A global way to match a give regular expression in the incoming request path.
801              
802             =head2 LocalRegex
803              
804             =head2 LocalRegexp
805              
806             B<Status: Deprecated.> Use Chained methods or other techniques.
807             If you really depend on this, install the standalone
808             L<Catalyst::DispatchType::Regex> distribution.
809              
810             Like L</Regex> but scoped under the namespace of the containing controller
811              
812             =head2 Chained
813              
814             =head2 ChainedParent
815              
816             =head2 PathPrefix
817              
818             =head2 PathPart
819              
820             =head2 CaptureArgs
821              
822             Allowed values for CaptureArgs is a single integer (CaptureArgs(2), meaning two
823             allowed) or you can declare a L<Moose>, L<MooseX::Types> or L<Type::Tiny>
824             named constraint such as CaptureArgs(Int,Str) would require two args with
825             the first being a Integer and the second a string. You may declare your own
826             custom type constraints and import them into the controller namespace:
827              
828             package MyApp::Controller::Root;
829              
830             use Moose;
831             use MooseX::MethodAttributes;
832             use MyApp::Types qw/Int/;
833              
834             extends 'Catalyst::Controller';
835              
836             sub chain_base :Chained(/) CaptureArgs(1) { }
837              
838             sub any_priority_chain :Chained(chain_base) PathPart('') Args(1) { }
839              
840             sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { }
841              
842             See L<Catalyst::RouteMatching> for more.
843              
844             Please see L<Catalyst::DispatchType::Chained> for more.
845              
846             =head2 ActionClass
847              
848             Set the base class for the action, defaults to L</Catalyst::Action>. It is now
849             preferred to use L</Does>.
850              
851             =head2 MyAction
852              
853             Set the ActionClass using a custom Action in your project namespace.
854              
855             The following is exactly the same:
856              
857             sub foo_action1 : Local ActionClass('+MyApp::Action::Bar') { ... }
858             sub foo_action2 : Local MyAction('Bar') { ... }
859              
860             =head2 Does
861              
862             package MyApp::Controller::Zoo;
863              
864             sub foo : Local Does('Buzz') { ... } # Catalyst::ActionRole::
865             sub bar : Local Does('~Buzz') { ... } # MyApp::ActionRole::Buzz
866             sub baz : Local Does('+MyApp::ActionRole::Buzz') { ... }
867              
868             =head2 GET
869              
870             =head2 POST
871              
872             =head2 PUT
873              
874             =head2 DELETE
875              
876             =head2 OPTION
877              
878             =head2 HEAD
879              
880             =head2 PATCH
881              
882             =head2 Method('...')
883              
884             Sets the give action path to match the specified HTTP method, or via one of the
885             broadly accepted methods of overriding the 'true' method (see
886             L<Catalyst::ActionRole::HTTPMethods>).
887              
888             =head2 Args
889              
890             When used with L</Path> indicates the number of arguments expected in
891             the path. However if no Args value is set, assumed to 'slurp' all
892             remaining path pars under this namespace.
893              
894             Allowed values for Args is a single integer (Args(2), meaning two allowed) or you
895             can declare a L<Moose>, L<MooseX::Types> or L<Type::Tiny> named constraint such
896             as Args(Int,Str) would require two args with the first being a Integer and the
897             second a string. You may declare your own custom type constraints and import
898             them into the controller namespace:
899              
900             package MyApp::Controller::Root;
901              
902             use Moose;
903             use MooseX::MethodAttributes;
904             use MyApp::Types qw/Tuple Int Str StrMatch UserId/;
905              
906             extends 'Catalyst::Controller';
907              
908             sub user :Local Args(UserId) {
909             my ($self, $c, $int) = @_;
910             }
911              
912             sub an_int :Local Args(Int) {
913             my ($self, $c, $int) = @_;
914             }
915              
916             sub many_ints :Local Args(ArrayRef[Int]) {
917             my ($self, $c, @ints) = @_;
918             }
919              
920             sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
921             my ($self, $c, $int) = @_;
922             }
923              
924             If you choose not to use imported type constraints (like L<Type::Tiny>, or <MooseX::Types>
925             you may use L<Moose> 'stringy' types however just like when you use these types in your
926             declared attributes you must quote them:
927              
928             sub my_moose_type :Local Args('Int') { ... }
929              
930             If you use 'reference' type constraints (such as ArrayRef[Int]) that have an unknown
931             number of allowed matches, we set this the same way "Args" is. Please keep in mind
932             that actions with an undetermined number of args match at lower precedence than those
933             with a fixed number. You may use reference types such as Tuple from L<Types::Standard>
934             that allows you to fix the number of allowed args. For example Args(Tuple[Int,Int])
935             would be determined to be two args (or really the same as Args(Int,Int).) You may
936             find this useful for creating custom subtypes with complex matching rules that you
937             wish to reuse over many actions.
938              
939             See L<Catalyst::RouteMatching> for more.
940              
941             B<Note>: It is highly recommended to use L<Type::Tiny> for your type constraints over
942             other options. L<Type::Tiny> exposed a better meta data interface which allows us to
943             do more and better types of introspection driving tests and debugging.
944              
945             =head2 Consumes('...')
946              
947             Matches the current action against the content-type of the request. Typically
948             this is used when the request is a POST or PUT and you want to restrict the
949             submitted content type. For example, you might have an HTML for that either
950             returns classic url encoded form data, or JSON when Javascript is enabled. In
951             this case you may wish to match either incoming type to one of two different
952             actions, for properly processing.
953              
954             Examples:
955              
956             sub is_json : Chained('start') Consumes('application/json') { ... }
957             sub is_urlencoded : Chained('start') Consumes('application/x-www-form-urlencoded') { ... }
958             sub is_multipart : Chained('start') Consumes('multipart/form-data') { ... }
959              
960             To reduce boilerplate, we include the following content type shortcuts:
961              
962             Examples
963              
964             sub is_json : Chained('start') Consume(JSON) { ... }
965             sub is_urlencoded : Chained('start') Consumes(UrlEncoded) { ... }
966             sub is_multipart : Chained('start') Consumes(Multipart) { ... }
967              
968             You may specify more than one match:
969              
970             sub is_more_than_one
971             : Chained('start')
972             : Consumes('application/x-www-form-urlencoded')
973             : Consumes('multipart/form-data')
974              
975             sub is_more_than_one
976             : Chained('start')
977             : Consumes(UrlEncoded)
978             : Consumes(Multipart)
979              
980             Since it is a common case the shortcut C<HTMLForm> matches both
981             'application/x-www-form-urlencoded' and 'multipart/form-data'. Here's the full
982             list of available shortcuts:
983              
984             JSON => 'application/json',
985             JS => 'application/javascript',
986             PERL => 'application/perl',
987             HTML => 'text/html',
988             XML => 'text/XML',
989             Plain => 'text/plain',
990             UrlEncoded => 'application/x-www-form-urlencoded',
991             Multipart => 'multipart/form-data',
992             HTMLForm => ['application/x-www-form-urlencoded','multipart/form-data'],
993              
994             Please keep in mind that when dispatching, L<Catalyst> will match the first most
995             relevant case, so if you use the C<Consumes> attribute, you should place your
996             most accurate matches early in the Chain, and your 'catchall' actions last.
997              
998             See L<Catalyst::ActionRole::ConsumesContent> for more.
999              
1000             =head2 Scheme(...)
1001              
1002             Allows you to specify a URI scheme for the action or action chain. For example
1003             you can required that a given path be C<https> or that it is a websocket endpoint
1004             C<ws> or C<wss>. For an action chain you may currently only have one defined
1005             Scheme.
1006              
1007             package MyApp::Controller::Root;
1008              
1009             use base 'Catalyst::Controller';
1010              
1011             sub is_http :Path(scheme) Scheme(http) Args(0) {
1012             my ($self, $c) = @_;
1013             $c->response->body("is_http");
1014             }
1015              
1016             sub is_https :Path(scheme) Scheme(https) Args(0) {
1017             my ($self, $c) = @_;
1018             $c->response->body("is_https");
1019             }
1020              
1021             In the above example http://localhost/root/scheme would match the first
1022             action (is_http) but https://localhost/root/scheme would match the second.
1023              
1024             As an added benefit, if an action or action chain defines a Scheme, when using
1025             $c->uri_for the scheme of the generated URL will use what you define in the action
1026             or action chain (the current behavior is to set the scheme based on the current
1027             incoming request). This makes it easier to use uri_for on websites where some
1028             paths are secure and others are not. You may also use this to other schemes
1029             like websockets.
1030              
1031             See L<Catalyst::ActionRole::Scheme> for more.
1032              
1033             =head1 OPTIONAL METHODS
1034              
1035             =head2 _parse_[$name]_attr
1036              
1037             Allows you to customize parsing of subroutine attributes.
1038              
1039             sub myaction1 :Path TwoArgs { ... }
1040              
1041             sub _parse_TwoArgs_attr {
1042             my ( $self, $c, $name, $value ) = @_;
1043             # $self -> controller instance
1044             #
1045             return(Args => 2);
1046             }
1047              
1048             Please note that this feature does not let you actually assign new functions
1049             to actions via subroutine attributes, but is really more for creating useful
1050             aliases to existing core and extended attributes, and transforms based on
1051             existing information (like from configuration). Code for actually doing
1052             something meaningful with the subroutine attributes will be located in the
1053             L<Catalyst::Action> classes (or your subclasses), L<Catalyst::Dispatcher> and
1054             in subclasses of L<Catalyst::DispatchType>. Remember these methods only get
1055             called basically once when the application is starting, not per request!
1056              
1057             =head1 AUTHORS
1058              
1059             Catalyst Contributors, see Catalyst.pm
1060              
1061             =head1 COPYRIGHT
1062              
1063             This library is free software. You can redistribute it and/or modify
1064             it under the same terms as Perl itself.
1065              
1066             =cut