1Catalyst::Component(3)User Contributed Perl DocumentationCatalyst::Component(3)
2
3
4
6 Catalyst::Component - Catalyst Component Base Class
7
9 # lib/MyApp/Model/Something.pm
10 package MyApp::Model::Something;
11
12 use base 'Catalyst::Component';
13
14 __PACKAGE__->config( foo => 'bar' );
15
16 sub test {
17 my $self = shift;
18 return $self->{foo};
19 }
20
21 sub forward_to_me {
22 my ( $self, $c ) = @_;
23 $c->response->output( $self->{foo} );
24 }
25
26 1;
27
28 # Methods can be a request step
29 $c->forward(qw/MyApp::Model::Something forward_to_me/);
30
31 # Or just methods
32 print $c->comp('MyApp::Model::Something')->test;
33
34 print $c->comp('MyApp::Model::Something')->{foo};
35
37 This is the universal base class for Catalyst components
38 (Model/View/Controller).
39
40 It provides you with a generic new() for instantiation through
41 Catalyst's component loader with config() support and a process()
42 method placeholder.
43
45 new($app, $arguments)
46 Called by COMPONENT to instantiate the component; should return an
47 object to be stored in the application's component hash.
48
49 COMPONENT
50 "my $component_instance = $component->COMPONENT($app, $arguments);"
51
52 If this method is present (as it is on all Catalyst::Component
53 subclasses), it is called by Catalyst during setup_components with the
54 application class as $app and any config entry on the application for
55 this component (for example, in the case of MyApp::Controller::Foo this
56 would be "MyApp->config('Controller::Foo' => \%conf").
57
58 The arguments are expected to be a hashref and are merged with the
59 "__PACKAGE__->config" hashref before calling "->new" to instantiate the
60 component.
61
62 You can override it in your components to do custom instantiation,
63 using something like this:
64
65 sub COMPONENT {
66 my ($class, $app, $args) = @_;
67 $args = $class->merge_config_hashes($class->config, $args);
68 return $class->new($app, $args);
69 }
70
71 $c->config
72 $c->config($hashref)
73 $c->config($key, $value, ...)
74 Accessor for this component's config hash. Config values can be set as
75 key value pair, or you can specify a hashref. In either case the keys
76 will be merged with any existing config settings. Each component in a
77 Catalyst application has its own config hash.
78
79 The component's config hash is merged with any config entry on the
80 application for this component and passed to "new()" (as mentioned
81 above at "COMPONENT"). The common practice to access the merged config
82 is to use a Moose attribute for each config entry on the receiving
83 component.
84
85 $c->process()
86 This is the default method called on a Catalyst component in the
87 dispatcher. For instance, Views implement this action to render the
88 response body when you forward to them. The default is an abstract
89 method.
90
91 $c->merge_config_hashes( $hashref, $hashref )
92 Merges two hashes together recursively, giving right-hand precedence.
93 Alias for the method in Catalyst::Utils.
94
95 $c->expand_modules( $setup_component_config )
96 Return a list of extra components that this component has created. By
97 default, it just looks for a list of inner packages of this component
98
100 ACCEPT_CONTEXT($c, @args)
101 Catalyst components are normally initialized during server startup,
102 either as a Class or a Instance. However, some components require
103 information about the current request. To do so, they can implement an
104 ACCEPT_CONTEXT method.
105
106 If this method is present, it is called during
107 $c->comp/controller/model/view with the current $c and any additional
108 args (e.g. $c->model('Foo', qw/bar baz/) would cause your
109 MyApp::Model::Foo instance's ACCEPT_CONTEXT to be called with ($c,
110 'bar', 'baz')) and the return value of this method is returned to the
111 calling code in the application rather than the component itself.
112
114 Catalyst, Catalyst::Model, Catalyst::View, Catalyst::Controller.
115
117 Catalyst Contributors, see Catalyst.pm
118
120 This library is free software. You can redistribute it and/or modify it
121 under the same terms as Perl itself.
122
123
124
125perl v5.12.1 2010-06-15 Catalyst::Component(3)