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 =head2 new($c, $arguments)
54 Called by COMPONENT to instantiate the component; should return an object
55 to be stored in the application's component hash.
60 my ( $self, $c ) = @_;
62 # Temporary fix, some components does not pass context to constructor
63 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
65 return $self->NEXT::new( { %{ $self->config }, %{$arguments} } );
68 =head2 COMPONENT($c, $arguments)
70 If this method is present (as it is on all Catalyst::Component subclasses,
71 it is called by Catalyst during setup_components with the application class
72 as $c and any config entry on the application for this component (for example,
73 in the case of MyApp::Controller::Foo this would be
74 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a hashref
75 and are merged with the __PACKAGE__->config hashref before calling ->new to
76 instantiate the component.
81 my ( $self, $c ) = @_;
83 # Temporary fix, some components does not pass context to constructor
84 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
86 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
90 if ( my $new = $self->new( $c, $arguments ) ) {
94 my $class = ref $self || $self;
95 my $new = { %{ $self->config }, %{$arguments} };
96 return bless $new, $class;
101 # remember to leave blank lines between the consecutive =head2's
102 # otherwise the pod tools don't recognize the subsequent =head2s
106 =head2 $c->config($hashref)
108 =head2 $c->config($key, $value, ...)
114 my $config = $self->_config;
116 $self->_config( $config = {} );
119 $config = { %{$config}, %{@_ > 1 ? {@_} : $_[0]} };
120 $self->_config($config);
131 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
132 . " did not override Catalyst::Component::process" );
135 =head1 OPTIONAL METHODS
137 =head2 ACCEPT_CONTEXT($c, @args)
139 Catalyst components are normally initalized during server startup, either
140 as a Class or a Instance. However, some components require information about
141 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
143 If this method is present, it is called during $c->comp/controller/model/view
144 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
145 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
146 ($c, 'bar', 'baz')) and the return value of this method is returned to the
147 calling code in the application rather than the component itself.
151 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
155 Sebastian Riedel, C<sri@cpan.org>
156 Marcus Ramberg, C<mramberg@cpan.org>
157 Matt S Trout, C<mst@shadowcatsystems.co.uk>
161 This program is free software, you can redistribute it and/or modify it under
162 the same terms as Perl itself.