1 package Catalyst::Base;
4 use base qw/Class::Data::Inheritable Class::Accessor::Fast/;
6 use Catalyst::Exception;
9 __PACKAGE__->mk_classdata($_) for qw/_attr_cache _action_cache _config/;
10 __PACKAGE__->_attr_cache( {} );
11 __PACKAGE__->_action_cache( [] );
13 # note - see attributes(3pm)
14 sub MODIFY_CODE_ATTRIBUTES {
15 my ( $class, $code, @attrs ) = @_;
16 $class->_attr_cache->{$code} = [@attrs];
17 push @{ $class->_action_cache }, [ $code, [@attrs] ];
21 sub FETCH_CODE_ATTRIBUTES { $_[0]->_attr_cache->{ $_[1] } || () }
25 Catalyst::Base - Catalyst Universal Base Class
29 # lib/MyApp/Model/Something.pm
30 package MyApp::Model::Something;
32 use base 'Catalyst::Base';
34 __PACKAGE__->config( foo => 'bar' );
42 my ( $self, $c ) = @_;
43 $c->response->output( $self->{foo} );
48 # Methods can be a request step
49 $c->forward(qw/MyApp::Model::Something forward_to_me/);
52 print $c->comp('MyApp::Model::Something')->test;
54 print $c->comp('MyApp::Model::Something')->{foo};
58 This is the universal base class for Catalyst components
59 (Model/View/Controller).
61 It provides you with a generic new() for instantiation through Catalyst's
62 component loader with config() support and a process() method placeholder.
73 my ( $self, $c ) = @_;
75 # Temporary fix, some components does not pass context to constructor
76 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
78 return $self->NEXT::new( { %{ $self->config }, %{ $arguments } } );
81 # remember to leave blank lines between the consecutive =item's
82 # otherwise the pod tools don't recognize the subsequent =items
86 =item $c->config($hashref)
88 =item $c->config($key, $value, ...)
94 $self->_config( {} ) unless $self->_config;
96 my $config = @_ > 1 ? {@_} : $_[0];
97 while ( my ( $key, $val ) = each %$config ) {
98 $self->_config->{$key} = $val;
101 return $self->_config;
110 Catalyst::Exception->throw(
111 message => ( ref $_[0] || $_[0] ) . " did not override Catalyst::Base::process"
115 =item FETCH_CODE_ATTRIBUTES
117 =item MODIFY_CODE_ATTRIBUTES
127 Sebastian Riedel, C<sri@cpan.org>
128 Marcus Ramberg, C<mramberg@cpan.org>
132 This program is free software, you can redistribute it and/or modify it under
133 the same terms as Perl itself.