1 package Catalyst::Component;
4 use base qw/Class::Accessor::Fast Class::Data::Inheritable/;
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 __PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
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(
63 $self->merge_config_hashes( $self->config, $arguments ) );
67 my ( $self, $c ) = @_;
69 # Temporary fix, some components does not pass context to constructor
70 my $arguments = ( ref( $_[-1] ) eq 'HASH' ) ? $_[-1] : {};
72 if ( my $new = $self->NEXT::COMPONENT( $c, $arguments ) ) {
76 if ( my $new = $self->new( $c, $arguments ) ) {
80 my $class = ref $self || $self;
81 my $new = $self->merge_config_hashes(
82 $self->config, $arguments );
83 return bless $new, $class;
90 my $config = $self->_config;
92 $self->_config( $config = {} );
95 my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
97 $self->merge_config_hashes( $config, $newconfig )
103 sub merge_config_hashes {
104 my ( $self, $lefthash, $righthash ) = @_;
106 return Catalyst::Utils::merge_hashes( $lefthash, $righthash );
111 Catalyst::Exception->throw( message => ( ref $_[0] || $_[0] )
112 . " did not override Catalyst::Component::process" );
121 =head2 new($c, $arguments)
123 Called by COMPONENT to instantiate the component; should return an object
124 to be stored in the application's component hash.
126 =head2 COMPONENT($c, $arguments)
128 If this method is present (as it is on all Catalyst::Component subclasses,
129 it is called by Catalyst during setup_components with the application class
130 as $c and any config entry on the application for this component (for example,
131 in the case of MyApp::Controller::Foo this would be
132 MyApp->config->{'Controller::Foo'}). The arguments are expected to be a
133 hashref and are merged with the __PACKAGE__->config hashref before calling
134 ->new to instantiate the component.
138 =head2 $c->config($hashref)
140 =head2 $c->config($key, $value, ...)
142 Accessor for this component's config hash. Config values can be set as
143 key value pair, or you can specify a hashref. In either case the keys
144 will be merged with any existing config settings. Each component in
145 a Catalyst application has it's own config hash.
149 This is the default method called on a Catalyst component in the dispatcher.
150 For instance, Views implement this action to render the response body
151 when you forward to them. The default is an abstract method.
153 =head2 $c->merge_config_hashes( $hashref, $hashref )
155 Merges two hashes together recursively, giving right-hand precedence.
156 Alias for the method in L<Catalyst::Utils>.
158 =head1 OPTIONAL METHODS
160 =head2 ACCEPT_CONTEXT($c, @args)
162 Catalyst components are normally initalized during server startup, either
163 as a Class or a Instance. However, some components require information about
164 the current request. To do so, they can implement an ACCEPT_CONTEXT method.
166 If this method is present, it is called during $c->comp/controller/model/view
167 with the current $c and any additional args (e.g. $c->model('Foo', qw/bar baz/)
168 would cause your MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with
169 ($c, 'bar', 'baz')) and the return value of this method is returned to the
170 calling code in the application rather than the component itself.
174 L<Catalyst>, L<Catalyst::Model>, L<Catalyst::View>, L<Catalyst::Controller>.
178 Sebastian Riedel, C<sri@cpan.org>
179 Marcus Ramberg, C<mramberg@cpan.org>
180 Matt S Trout, C<mst@shadowcatsystems.co.uk>
184 This program is free software, you can redistribute it and/or modify it under
185 the same terms as Perl itself.