1 package Catalyst::Component;
4 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
7 __PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
11 Catalyst::Component - Catalyst Component Base Class
15 # lib/MyApp/Model/Something.pm
16 package MyApp::Model::Something;
18 use base 'Catalyst::Component';
20 __PACKAGE__->config( foo => 'bar' );
28 my ( $self, $c ) = @_;
29 $c->response->output( $self->{foo} );
34 # Methods can be a request step
35 $c->forward(qw/MyApp::Model::Something forward_to_me/);
38 print $c->comp('MyApp::Model::Something')->test;
40 print $c->comp('MyApp::Model::Something')->{foo};
44 This is the universal base class for Catalyst components
45 (Model/View/Controller).
47 It provides you with a generic new() for instantiation through Catalyst's
48 component loader with config() support and a process() method placeholder.
57 my ( $self, $c ) = @_;
59 # Temporary fix, some components does not pass context to constructor
60 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
62 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
70 my ( $self, $c ) = @_;
72 # Temporary fix, some components does not pass context to constructor
73 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
75 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
79 if ( my $new = $self->new( $c, $arguments ) ) {
83 my $class = ref $self || $self;
84 my $new = { %{ $self->config }, %{$arguments} };
85 return bless $new, $class;
90 # remember to leave blank lines between the consecutive =head2's
91 # otherwise the pod tools don't recognize the subsequent =head2s
95 =head2 $c->config($hashref)
97 =head2 $c->config($key, $value, ...)
103 $self->_config( {} ) unless $self->_config;
105 my $config = @_ > 1 ? {@_} : $_[0];
106 while ( my ( $key, $val ) = each %$config ) {
107 $self->_config->{$key} = $val;
110 return $self->_config;
119 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
120 . " did not override Catalyst::Component::process" );
125 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
129 Sebastian Riedel, C<sri@cpan.org>
130 Marcus Ramberg, C<mramberg@cpan.org>
131 Matt S Trout, C<mst@shadowcatsystems.co.uk>
135 This program is free software, you can redistribute it and/or modify it under
136 the same terms as Perl itself.