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.
52 Catalyst components are normally initalized during server startup, either
53 as a Class or a Instance. However, some compoents require information about
54 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
56 The ACCEPT_CONTEXT method is called on the component as initalized at startup,
57 with the current $c object, and should return itself. It can do whatever it
58 likes with $c, such as extracting a path to use in the component or something
61 This call happens for every $c->comp/controller/model/view call.
71 my ( $self, $c ) = @_;
73 # Temporary fix, some components does not pass context to constructor
74 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
76 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
84 my ( $self, $c ) = @_;
86 # Temporary fix, some components does not pass context to constructor
87 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
89 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
93 if ( my $new = $self->new( $c, $arguments ) ) {
97 my $class = ref $self || $self;
98 my $new = { %{ $self->config }, %{$arguments} };
99 return bless $new, $class;
104 # remember to leave blank lines between the consecutive =head2's
105 # otherwise the pod tools don't recognize the subsequent =head2s
109 =head2 $c->config($hashref)
111 =head2 $c->config($key, $value, ...)
117 my $config = $self->_config;
119 $self->_config( $config = {} );
122 $config = { %{$config}, %{@_ > 1 ? {@_} : $_[0]} };
123 $self->_config($config);
134 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
135 . " did not override Catalyst::Component::process" );
140 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
144 Sebastian Riedel, C<sri@cpan.org>
145 Marcus Ramberg, C<mramberg@cpan.org>
146 Matt S Trout, C<mst@shadowcatsystems.co.uk>
150 This program is free software, you can redistribute it and/or modify it under
151 the same terms as Perl itself.