1 package Catalyst::Component;
4 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
8 __PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
12 Catalyst::Component - Catalyst Component Base Class
16 # lib/MyApp/Model/Something.pm
17 package MyApp::Model::Something;
19 use base 'Catalyst::Component';
21 __PACKAGE__->config( foo => 'bar' );
29 my ( $self, $c ) = @_;
30 $c->response->output( $self->{foo} );
35 # Methods can be a request step
36 $c->forward(qw/MyApp::Model::Something forward_to_me/);
39 print $c->comp('MyApp::Model::Something')->test;
41 print $c->comp('MyApp::Model::Something')->{foo};
45 This is the universal base class for Catalyst components
46 (Model/View/Controller).
48 It provides you with a generic new() for instantiation through Catalyst's
49 component loader with config() support and a process() method placeholder.
53 =head2 new($c, $arguments)
55 Called by COMPONENT to instantiate the component; should return an object
56 to be stored in the application's component hash.
61 my ( $self, $c ) = @_;
63 # Temporary fix, some components does not pass context to constructor
64 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
66 return $self->NEXT::new( $self->merge_config_hashes( $self->config, $arguments ) );
69 =head2 COMPONENT($c, $arguments)
71 If this method is present (as it is on all Catalyst::Component subclasses,
72 it is called by Catalyst during setup_components with the application class
73 as $c and any config entry on the application for this component (for example,
74 in the case of MyApp::Controller::Foo this would be
75 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a hashref
76 and are merged with the __PACKAGE__->config hashref before calling ->new to
77 instantiate the component.
82 my ( $self, $c ) = @_;
84 # Temporary fix, some components does not pass context to constructor
85 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
87 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
91 if ( my $new = $self->new( $c, $arguments ) ) {
95 my $class = ref $self || $self;
96 my $new = $self->merge_config_hashes( $self->config, $arguments );
97 return bless $new, $class;
102 # remember to leave blank lines between the consecutive =head2's
103 # otherwise the pod tools don't recognize the subsequent =head2s
107 =head2 $c->config($hashref)
109 =head2 $c->config($key, $value, ...)
115 my $config = $self->_config;
117 $self->_config( $config = {} );
120 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
122 $self->merge_config_hashes( $config, $newconfig )
134 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
135 . " did not override Catalyst::Component::process" );
138 =head2 $c->merge_config_hashes( $hashref, $hashref )
140 Merges two hashes together recursively, giving right-hand precedence.
144 sub merge_config_hashes {
145 my ( $self, $lefthash, $righthash ) = @_;
147 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
150 =head1 OPTIONAL METHODS
152 =head2 ACCEPT_CONTEXT($c, @args)
154 Catalyst components are normally initalized during server startup, either
155 as a Class or a Instance. However, some components require information about
156 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
158 If this method is present, it is called during $c->comp/controller/model/view
159 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
160 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
161 ($c, 'bar', 'baz')) and the return value of this method is returned to the
162 calling code in the application rather than the component itself.
166 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
170 Sebastian Riedel, C<sri@cpan.org>
171 Marcus Ramberg, C<mramberg@cpan.org>
172 Matt S Trout, C<mst@shadowcatsystems.co.uk>
176 This program is free software, you can redistribute it and/or modify it under
177 the same terms as Perl itself.