1 package Catalyst::Controller;
3 #switch to BEGIN { extends qw/ ... /; } ?
6 use base qw/Catalyst::Component Catalyst::AttrContainer/;
9 use Scalar::Util qw/blessed/;
10 use Catalyst::Exception;
19 predicate => 'has_path_prefix',
22 has action_namespace =>
26 init_arg => 'namespace',
27 predicate => 'has_action_namespace',
37 # isa => 'ClassName|Catalyst' ?
38 has _application => (is => 'rw');
39 sub _app{ shift->_application(@_) }
42 my ($self, $args) = @_;
43 my $action = delete $args->{action} || {};
44 my $actions = delete $args->{actions} || {};
45 my $attr_value = $self->merge_config_hashes($actions, $action);
46 $self->actions($attr_value);
51 Catalyst::Controller - Catalyst Controller base class
55 package MyApp::Controller::Search
56 use base qw/Catalyst::Controller/;
59 my ($self,$c,@args) = @_;
61 } # Dispatches to /search/foo
65 Controllers are where the actions in the Catalyst framework
66 reside. Each action is represented by a function with an attribute to
67 identify what kind of action it is. See the L<Catalyst::Dispatcher>
68 for more info about how Catalyst dispatches to actions.
72 #I think both of these could be attributes. doesn't really seem like they need
73 #to ble class data. i think that attributes +default would work just fine
74 __PACKAGE__->mk_classdata($_) for qw/_dispatch_steps _action_class/;
76 __PACKAGE__->_dispatch_steps( [qw/_BEGIN _AUTO _ACTION/] );
77 __PACKAGE__->_action_class('Catalyst::Action');
80 sub _DISPATCH : Private {
81 my ( $self, $c ) = @_;
83 foreach my $disp ( @{ $self->_dispatch_steps } ) {
84 last unless $c->forward($disp);
90 sub _BEGIN : Private {
91 my ( $self, $c ) = @_;
92 my $begin = ( $c->get_actions( 'begin', $c->namespace ) )[-1];
93 return 1 unless $begin;
94 $begin->dispatch( $c );
95 return !@{ $c->error };
99 my ( $self, $c ) = @_;
100 my @auto = $c->get_actions( 'auto', $c->namespace );
101 foreach my $auto (@auto) {
102 $auto->dispatch( $c );
103 return 0 unless $c->state;
108 sub _ACTION : Private {
109 my ( $self, $c ) = @_;
111 && $c->action->can('execute')
114 $c->action->dispatch( $c );
116 return !@{ $c->error };
120 my ( $self, $c ) = @_;
121 my $end = ( $c->get_actions( 'end', $c->namespace ) )[-1];
122 return 1 unless $end;
123 $end->dispatch( $c );
124 return !@{ $c->error };
130 my $new = $self->next::method(@_);
131 $new->_application( $app );
136 my ( $self, $name ) = @_;
137 my $app = ($self->isa('Catalyst') ? $self : $self->_application);
138 return $app->dispatcher->get_action($name, $self->action_namespace);
141 #my opinion is that this whole sub really should be a builder method, not
142 #something that happens on every call. Anyone else disagree?? -- groditi
143 ## -- apparently this is all just waiting for app/ctx split
144 around action_namespace => sub {
146 my ( $self, $c ) = @_;
149 return $self->$orig if $self->has_action_namespace;
151 return $self->config->{namespace} if exists $self->config->{namespace};
156 $case_s = $c->config->{case_sensitive};
158 if ($self->isa('Catalyst')) {
159 $case_s = $self->config->{case_sensitive};
162 $case_s = $self->_application->config->{case_sensitive};
164 confess("Can't figure out case_sensitive setting");
169 my $namespace = Catalyst::Utils::class2prefix(ref($self) || $self, $case_s) || '';
170 $self->$orig($namespace) if ref($self);
174 #Once again, this is probably better written as a builder method
175 around path_prefix => sub {
179 return $self->$orig if $self->has_path_prefix;
181 return $self->config->{path} if exists $self->config->{path};
183 my $namespace = $self->action_namespace(@_);
184 $self->$orig($namespace) if ref($self);
189 sub register_actions {
190 my ( $self, $c ) = @_;
191 my $class = ref $self || $self;
192 #this is still not correct for some reason.
193 my $namespace = $self->action_namespace($c);
194 my $meta = $self->meta;
195 my %methods = map{ $_->{code}->body => $_->{name} }
196 grep {$_->{class} ne 'Moose::Object'} #ignore Moose::Object methods
197 $meta->compute_all_applicable_methods;
200 # Advanced inheritance support for plugins and the like
201 #moose todo: migrate to eliminate CDI compat
203 for my $isa ( $meta->superclasses, $class ) {
204 if(my $coderef = $isa->can('_action_cache')){
205 push(@action_cache, @{ $isa->$coderef });
209 foreach my $cache (@action_cache) {
210 my $code = $cache->[0];
211 my $method = delete $methods{$code}; # avoid dupe registers
213 my $attrs = $self->_parse_attrs( $c, $method, @{ $cache->[1] } );
214 if ( $attrs->{Private} && ( keys %$attrs > 1 ) ) {
215 $c->log->debug( 'Bad action definition "'
216 . join( ' ', @{ $cache->[1] } )
217 . qq/" for "$class->$method"/ )
221 my $reverse = $namespace ? "${namespace}/${method}" : $method;
222 my $action = $self->create_action(
226 namespace => $namespace,
228 attributes => $attrs,
231 $c->dispatcher->register( $c, $action );
239 my $class = (exists $args{attributes}{ActionClass}
240 ? $args{attributes}{ActionClass}[0]
241 : $self->_action_class);
243 Class::MOP::load_class($class);
244 return $class->new( \%args );
248 my ( $self, $c, $name, @attrs ) = @_;
252 foreach my $attr (@attrs) {
254 # Parse out :Foo(bar) into Foo => bar etc (and arrayify)
256 if ( my ( $key, $value ) = ( $attr =~ /^(.*?)(?:\(\s*(.+?)\s*\))?$/ ) )
259 if ( defined $value ) {
260 ( $value =~ s/^'(.*)'$/$1/ ) || ( $value =~ s/^"(.*)"/$1/ );
262 push( @{ $raw_attributes{$key} }, $value );
266 #I know that the original behavior was to ignore action if actions was set
267 # but i actually think this may be a little more sane? we can always remove
268 # the merge behavior quite easily and go back to having actions have
269 # presedence over action by modifying the keys. i honestly think this is
270 # superior while mantaining really high degree of compat
273 $actions = $self->actions;
275 my $cfg = $self->config;
276 $actions = $self->merge_config_hashes($cfg->{actions}, $cfg->{action});
279 %raw_attributes = ((exists $actions->{'*'} ? %{$actions->{'*'}} : ()),
281 (exists $actions->{$name} ? %{$actions->{$name}} : ()));
284 my %final_attributes;
286 foreach my $key (keys %raw_attributes) {
288 my $raw = $raw_attributes{$key};
290 foreach my $value (ref($raw) eq 'ARRAY' ? @$raw : $raw) {
292 my $meth = "_parse_${key}_attr";
293 if ( my $code = $self->can($meth) ) {
294 ( $key, $value ) = $self->$code( $c, $name, $value );
296 push( @{ $final_attributes{$key} }, $value );
300 return \%final_attributes;
303 sub _parse_Global_attr {
304 my ( $self, $c, $name, $value ) = @_;
305 return $self->_parse_Path_attr( $c, $name, "/$name" );
308 sub _parse_Absolute_attr { shift->_parse_Global_attr(@_); }
310 sub _parse_Local_attr {
311 my ( $self, $c, $name, $value ) = @_;
312 return $self->_parse_Path_attr( $c, $name, $name );
315 sub _parse_Relative_attr { shift->_parse_Local_attr(@_); }
317 sub _parse_Path_attr {
318 my ( $self, $c, $name, $value ) = @_;
320 if ( $value =~ m!^/! ) {
321 return ( 'Path', $value );
323 elsif ( length $value ) {
324 return ( 'Path', join( '/', $self->path_prefix($c), $value ) );
327 return ( 'Path', $self->path_prefix($c) );
331 sub _parse_Regex_attr {
332 my ( $self, $c, $name, $value ) = @_;
333 return ( 'Regex', $value );
336 sub _parse_Regexp_attr { shift->_parse_Regex_attr(@_); }
338 sub _parse_LocalRegex_attr {
339 my ( $self, $c, $name, $value ) = @_;
340 unless ( $value =~ s/^\^// ) { $value = "(?:.*?)$value"; }
341 return ( 'Regex', '^' . $self->path_prefix($c) . "/${value}" );
344 sub _parse_LocalRegexp_attr { shift->_parse_LocalRegex_attr(@_); }
346 sub _parse_ActionClass_attr {
347 my ( $self, $c, $name, $value ) = @_;
348 unless ( $value =~ s/^\+// ) {
349 $value = join('::', $self->_action_class, $value );
351 return ( 'ActionClass', $value );
354 sub _parse_MyAction_attr {
355 my ( $self, $c, $name, $value ) = @_;
357 my $appclass = Catalyst::Utils::class2appclass($self);
358 $value = "${appclass}::Action::${value}";
360 return ( 'ActionClass', $value );
371 Like any other L<Catalyst::Component>, controllers have a config hash,
372 accessible through $self->config from the controller actions. Some
373 settings are in use by the Catalyst framework:
377 This specifies the internal namespace the controller should be bound
378 to. By default the controller is bound to the URI version of the
379 controller name. For instance controller 'MyApp::Controller::Foo::Bar'
380 will be bound to 'foo/bar'. The default Root controller is an example
381 of setting namespace to '' (the null string).
385 Sets 'path_prefix', as described below.
389 =head2 $class->new($app, @args)
391 Proxies through to NEXT::new and stashes the application instance as
394 =head2 $self->action_for('name')
396 Returns the Catalyst::Action object (if any) for a given method name
399 =head2 $self->register_actions($c)
401 Finds all applicable actions for this component, creates
402 Catalyst::Action objects (using $self->create_action) for them and
403 registers them with $c->dispatcher.
405 =head2 $self->action_namespace($c)
407 Returns the private namespace for actions in this component. Defaults
408 to a value from the controller name (for
409 e.g. MyApp::Controller::Foo::Bar becomes "foo/bar") or can be
410 overridden from the "namespace" config key.
413 =head2 $self->path_prefix($c)
415 Returns the default path prefix for :Local, :LocalRegex and relative
416 :Path actions in this component. Defaults to the action_namespace or
417 can be overridden from the "path" config key.
419 =head2 $self->create_action(%args)
421 Called with a hash of data to be use for construction of a new
422 Catalyst::Action (or appropriate sub/alternative class) object.
424 Primarily designed for the use of register_actions.
426 =head2 $self->_application
430 Returns the application instance stored by C<new()>
434 Sebastian Riedel, C<sri@oook.de>
435 Marcus Ramberg C<mramberg@cpan.org>
439 This program is free software, you can redistribute it and/or modify
440 it under the same terms as Perl itself.