Expand roles section to contain specific docs on caveats, and version numbers when...
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / ExtendingCatalyst.pod
CommitLineData
38017482 1=head1 NAME
2
3Catalyst::Manual::ExtendingCatalyst - Extending The Framework
4
5=head1 DESCRIPTION
6
7This document will provide you with access points, techniques and best
b7c570ac 8practices to extend the L<Catalyst> framework, or to find more elegant
9ways to abstract and use your own code.
38017482 10
b7c570ac 11The design of Catalyst is such that the framework itself should not
12get in your way. There are many entry points to alter or extend
13Catalyst's behaviour, and this can be confusing. This document is
14written to help you understand the possibilities, current practices
15and their consequences.
38017482 16
17Please read the L<BEST PRACTICES> section before deciding on a design,
b7c570ac 18especially if you plan to release your code to CPAN. The Catalyst
19developer and user communities, which B<you are part of>, will benefit
20most if we all work together and coordinate.
21
22If you are unsure on an implementation or have an idea you would like
23to have RFC'ed, it surely is a good idea to send your questions and
24suggestions to the Catalyst mailing list (See L<Catalyst/SUPPORT>)
25and/or come to the C<#catalyst> channel on the C<irc.perl.org>
26network. You might also want to refer to those places for research to
27see if a module doing what you're trying to implement already
28exists. This might give you a solution to your problem or a basis for
29starting.
38017482 30
31=head1 BEST PRACTICES
32
b7c570ac 33During Catalyst's early days, it was common to write plugins to
34provide functionality application wide. Since then, Catalyst has
35become a lot more flexible and powerful. It soon became a best
36practice to use some other form of abstraction or interface, to keep
37the scope of its influence as close as possible to where it belongs.
38017482 38
b7c570ac 39For those in a hurry, here's a quick checklist of some fundamental
40points. If you are going to read the whole thing anyway, you can jump
38017482 41forward to L</Namespaces>.
42
43=head2 Quick Checklist
44
45=over
46
47=item Use the C<CatalystX::*> namespace if you can!
48
78170776 49If your extension isn't a Model, View, Controller, Plugin, Engine,
50or Log, it's best to leave it out of the C<Catalyst::> namespace.
51Use <CatalystX::> instead.
38017482 52
53=item Don't make it a plugin unless you have to!
54
1972ebdd 55A plugin should be careful since it's overriding Catalyst internals.
56If your plugin doesn't really need to muck with the internals, make it a
57base Controller or Model.
38017482 58
fa025310 59Also, if you think you really need a plugin, please instead consider
60using a L<Moose::Role>.
78170776 61
38017482 62=item There's a community. Use it!
63
b7c570ac 64There are many experienced developers in the Catalyst community,
65there's always the IRC channel and the mailing list to discuss things.
38017482 66
67=item Add tests and documentation!
68
b7c570ac 69This gives a stable basis for contribution, and even more importantly,
70builds trust. The easiest way is a test application. See
38017482 71L<Catalyst::Manual::Tutorial::Testing> for more information.
72
73=back
74
75=head2 Namespaces
76
b7c570ac 77While some core extensions (engines, plugins, etc.) have to be placed
78in the C<Catalyst::*> namespace, the Catalyst core would like to ask
38017482 79developers to use the C<CatalystX::*> namespace if possible.
80
fa025310 81Please B<do not> invent components which are outside the well
82known C<Model>, C<View>, C<Controller> or C<Plugin> namespaces!
83
b7c570ac 84When you try to put a base class for a C<Model>, C<View> or
85C<Controller> directly under your C<MyApp> directory as, for example,
86C<MyApp::Controller::Foo>, you will have the problem that Catalyst
87will try to load that base class as a component of your
88application. The solution is simple: Use another namespace. Common
89ones are C<MyApp::Base::Controller::*> or C<MyApp::ControllerBase::*>
90as examples.
38017482 91
92=head2 Can it be a simple module?
93
b7c570ac 94Sometimes you want to use functionality in your application that
95doesn't require the framework at all. Remember that Catalyst is just
96Perl and you always can just C<use> a module. If you have application
97specific code that doesn't need the framework, there is no problem in
98putting it in your C<MyApp::*> namespace. Just don't put it in
99C<Model>, C<Controller> or C<View>, because that would make Catalyst
100try to load them as components.
38017482 101
1972ebdd 102Writing a generic component that only works with Catalyst is wasteful
103of your time. Try writing a plain perl module, and then a small bit
104of glue that integrates it with Catalyst. See
105L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema> for a
106module that takes the approach. The advantage here is that your
107"Catalyst" DBIC schema works perfectly outside of Catalyst, making
108testing (and command-line scripts) a breeze. The actual Catalyst
109Model is just a few lines of glue that makes working with the schema
110convenient.
111
7d36d4ac 112If you want the thinnest interface possible, take a look at
113L<Catalyst::Model::Adaptor|Catalyst::Model::Adaptor>.
114
78170776 115=head2 Using Moose roles to apply method modifiers
116
117Rather than having a complex set of base classes which you have to mixin
118via multiple inheritence, if your functionality is well structured, then
119it's possible to use the composability of L<Moose> roles, and method modifiers
fa025310 120to hook onto to provide functionality.
78170776 121
23cf3a36 122These can be applied to your models/views/controllers, and your application
123class. Please see the sections below for special notes and caveats, and
124the L<Moose::Manual::Roles> for more information about roles in general.
125
126=head3 In your application class
127
128It should be noted that when applying roles to your application class, that
129you should B<not> wrap methods provided by L<Catalyst> until B<after> you have
130run C<< __PACKAGE__->setup >>, as your class does not inherit from any of your
131plugins until the setup method is run.
132
133With Catalyst 5.80004, it is possible to include Roles in the plugin list, and
134these roles will be applied to your application class immediately after
135'traditional' plugins have been composed into your application class'
136inheritance hierarchy.
137
138=head3 In controllers
139
140Method modifiers on controller actions will work as expected (either in your
141controllers, or when composed from roles) in Catalyst 5.80003 and above.
142
143It is possible to have action methods with attributes inside Moose roles, using
144the trait introduced in L<MooseX::MethodAttributes> version 0.12, example:
145
146 package MyApp::ControllerRole;
147 use Moose::Role -traits => 'MethodAttributes';
148
149 sub foo : Local {
150 my ($self, $c) = @_;
151 ...
152 }
78170776 153
38017482 154=head2 Inheritance and overriding methods
155
23cf3a36 156When overriding a method, keep in mind that some day additionall
38017482 157arguments may be provided to the method, if the last parameter is not
158a flat list. It is thus better to override a method by shifting the
159invocant off of C<@_> and assign the rest of the used arguments, so
160you can pass your complete arguments to the original method via C<@_>:
161
20a4dd98 162 use MRO::Compat; ...
38017482 163
fa025310 164 sub foo {
165 my $self = shift;
166 my ($bar, $baz) = @_; # ... return
167 $self->next::method(@_);
168 }
38017482 169
170If you would do the common
171
172 my ($self, $foo, $bar) = @_;
173
174you'd have to use a much uglier construct to ensure that all arguments
175will be passed along and the method is future proof:
176
177 $self->next::method(@_[ 1 .. $#_ ]);
178
179=head2 Tests and documentation
180
b7c570ac 181When you release your module to the CPAN, proper documentation and at
182least a basic test suite (which means more than pod or even just
183C<use_ok>, sorry) gives people a good base to contribute to the
184module. It also shows that you care for your users. If you would like
185your module to become a recommended addition, these things will prove
38017482 186invaluable.
187
1972ebdd 188If you're just getting started, try using
189L<CatalystX::Starter|CatalystX::Starter> to generate some example
190tests for your module.
191
38017482 192=head2 Maintenance
193
b7c570ac 194In planning to release a module to the community (Catalyst or CPAN and
195Perl), you should consider if you have the resources to keep it up to
196date, including fixing bugs and accepting contributions.
38017482 197
b7c570ac 198If you're not sure about this, you can always ask in the proper
199Catalyst or Perl channels if someone else might be interested in the
200project, and would jump in as co-maintainer.
38017482 201
b7c570ac 202A public repository can further ease interaction with the
203community. Even read only access enables people to provide you with
204patches to your current development version. subversion, SVN and SVK,
205are broadly preferred in the Catalyst community.
38017482 206
b7c570ac 207If you're developing a Catalyst extension, please consider asking the
208core team for space in Catalyst's own subversion repository. You can
209get in touch about this via IRC or the Catalyst developers mailing
210list.
38017482 211
212=head2 The context object
213
214Sometimes you want to get a hold of the context object in a component
b7c570ac 215that was created on startup time, where no context existed yet. Often
38017482 216this is about the model reading something out of the stash or other
b7c570ac 217context information (current language, for example).
38017482 218
b7c570ac 219If you use the context object in your component you have tied it to an
220existing request. This means that you might get into problems when
221you try to use the component (e.g. the model - the most common case)
222outside of Catalyst, for example in cronjobs.
38017482 223
b7c570ac 224A stable solution to this problem is to design the Catalyst model
225separately from the underlying model logic. Let's take
226L<Catalyst::Model::DBIC::Schema> as an example. You can create a
38017482 227schema outside of Catalyst that knows nothing about the web. This kind
228of design ensures encapsulation and makes development and maintenance
229a whole lot easier. The you use the aforementioned model to tie your
b7c570ac 230schema to your application. This gives you a C<MyApp::DBIC> (the name
231is of course just an example) model as well as
232C<MyApp::DBIC::TableName> models to access your result sources
233directly.
234
235By creating such a thin layer between the actual model and the
236Catalyst application, the schema itself is not at all tied to any
237application and the layer in-between can access the model's API using
238information from the context object.
239
240A Catalyst component accesses the context object at request time with
38017482 241L<Catalyst::Component/"ACCEPT_CONTEXT($c, @args)">.
242
243=head1 CONFIGURATION
244
b7c570ac 245The application has to interact with the extension with some
246configuration. There is of course again more than one way to do it.
38017482 247
248=head2 Attributes
249
b7c570ac 250You can specify any valid Perl attribute on Catalyst actions you like.
251(See L<attributes/"Syntax of Attribute Lists"> for a description of
252what is valid.) These will be available on the C<Catalyst::Action>
253instance via its C<attributes> accessor. To give an example, this
254action:
38017482 255
256 sub foo : Local Bar('Baz') {
257 my ($self, $c) = @_;
b7c570ac 258 my $attributes =
259 $self->action_for('foo')->attributes;
260 $c->res->body($attributes->{Bar}[0] );
38017482 261 }
262
b7c570ac 263will set the response body to C<Baz>. The values always come in an
264array reference. As you can see, you can use attributes to configure
265your actions. You can specify or alter these attributes via
266L</"Component Configuration">, or even react on them as soon as
267Catalyst encounters them by providing your own L<component base
268class|/"Component Base Classes">.
38017482 269
270=head2 Creating custom accessors
271
b7c570ac 272L<Catalyst::Component> uses L<Class::Accessor::Fast> for accessor
273creation. Please refer to the modules documentation for usage
38017482 274information.
275
276=head2 Component configuration
277
b7c570ac 278At creation time, the class configuration of your component (the one
279available via C<$self-E<gt>config>) will be merged with possible
38017482 280configuration settings from the applications configuration (either
b7c570ac 281directly or via config file). This is then stored in the controller
282object's hash reference. So, if you read possible configurations like:
38017482 283
284 my $model_name = $controller->{model_name};
285
b7c570ac 286you will get the right value. The C<config> accessor always only
38017482 287contains the original class configuration and must not be used for
288component configuration.
289
290You are advised to create accessors on your component class for your
291configuration values. This is good practice and makes it easier to
b7c570ac 292capture configuration key typos. You can do this with the
38017482 293C<mk_ro_accessors> method provided to L<Catalyst::Component> via
294L<Class::Accessor::Fast>:
295
296 use base 'Catalyst::Controller';
297 __PACKAGE__->mk_ro_accessors('model_name');
298 ...
299 my $model_name = $controller->model_name;
300
301=head1 IMPLEMENTATION
302
b7c570ac 303This part contains the technical details of various implementation
38017482 304methods. Please read the L</"BEST PRACTICES"> before you start your
305implementation, if you haven't already.
306
307=head2 Action classes
308
309Usually, your action objects are of the class L<Catalyst::Action>.
310You can override this with the C<ActionClass> attribute to influence
b7c570ac 311execution and/or dispatching of the action. A widely used example of
312this is L<Catalyst::Action::RenderView>, which is used in every newly
313created Catalyst application in your root controller:
38017482 314
315 sub end : ActionClass('RenderView') { }
316
b7c570ac 317Usually, you want to override the C<execute> and/or the C<match>
318method. The execute method of the action will naturally call the
319methods code. You can surround this by overriding the method in a
320subclass:
38017482 321
20a4dd98 322 package Catalyst::Action::MyFoo;
323 use strict;
38017482 324
20a4dd98 325 use MRO::Compat;
326 use base 'Catalyst::Action';
38017482 327
328 sub execute {
329 my $self = shift;
330 my ($controller, $c, @args) = @_;
38017482 331 # put your 'before' code here
332 my $r = $self->next::method(@_);
333 # put your 'after' code here
38017482 334 return $r;
335 }
38017482 336 1;
337
20a4dd98 338We are using L<MRO::Compat> to ensure that you have the next::method
339call, from L<Class::C3> (in older perls), or natively (if you are using
340perl 5.10) to re-dispatch to the original C<execute> method in the
341L<Catalyst::Action> class.
38017482 342
b7c570ac 343The Catalyst dispatcher handles an incoming request and, depending
344upon the dispatch type, will call the appropriate target or chain.
345From time to time it asks the actions themselves, or through the
346controller, if they would match the current request. That's what the
347C<match> method does. So by overriding this, you can change on what
348the action will match and add new matching criteria.
38017482 349
b7c570ac 350For example, the action class below will make the action only match on
351Mondays:
38017482 352
78170776 353 package Catalyst::Action::OnlyMondays;
354 use strict;
38017482 355
20a4dd98 356 use MRO::Compat;
38017482 357 use base 'Catalyst::Action';
358
359 sub match {
360 my $self = shift;
361 return 0 if ( localtime(time) )[6] == 1;
362 return $self->next::method(@_);
b7c570ac 363 }
38017482 364 1;
365
366And this is how we'd use it:
367
368 sub foo: Local ActionClass('OnlyMondays') {
369 my ($self, $c) = @_;
370 $c->res->body('I feel motivated!');
371 }
372
b7c570ac 373If you are using action classes often or have some specific base
374classes that you want to specify more conveniently, you can implement
375a component base class providing an attribute handler.
38017482 376
b7c570ac 377For further information on action classes, please refer to
38017482 378L<Catalyst::Action> and L<Catalyst::Manual::Actions>.
379
380=head2 Component base classes
381
b7c570ac 382Many L<Catalyst::Plugin> that were written in Catalyst's early days
383should really have been just controller base classes. With such a
384class, you could provide functionality scoped to a single controller,
385not polluting the global namespace in the context object.
38017482 386
b7c570ac 387You can provide regular Perl methods in a base class as well as
388actions which will be inherited to the subclass. Please refer to
389L</Controllers> for an example of this.
38017482 390
b7c570ac 391You can introduce your own attributes by specifying a handler method
392in the controller base. For example, to use a C<FullClass> attribute
393to specify a fully qualified action class name, you could use the
394following implementation. Note, however, that this functionality is
395already provided via the C<+> prefix for action classes. A simple
38017482 396
397 sub foo : Local ActionClass('+MyApp::Action::Bar') { ... }
398
399will use C<MyApp::Action::Bar> as action class.
400
b7c570ac 401 package MyApp::Base::Controller::FullClass; use strict; use base
402 'Catalyst::Controller';
38017482 403
404 sub _parse_FullClass_attr {
405 my ($self, $app_class, $action_name, $value, $attrs) = @_;
406 return( ActionClass => $value );
407 }
38017482 408 1;
409
b7c570ac 410Note that the full line of arguments is only provided for completeness
411sake. We could use this attribute in a subclass like any other
412Catalyst attribute:
38017482 413
414 package MyApp::Controller::Foo;
415 use strict;
416 use base 'MyApp::Base::Controller::FullClass';
417
418 sub foo : Local FullClass('MyApp::Action::Bar') { ... }
419
420 1;
421
422=head2 Controllers
423
b7c570ac 424Many things can happen in controllers, and it often improves
425maintainability to abstract some of the code out into reusable base
38017482 426classes.
427
428You can provide usual Perl methods that will be available via your
b7c570ac 429controller object, or you can even define Catalyst actions which will
430be inherited by the subclasses. Consider this controller base class:
38017482 431
432 package MyApp::Base::Controller::ModelBase;
433 use strict;
434 use base 'Catalyst::Controller';
435
436 sub list : Chained('base') PathPart('') Args(0) {
437 my ($self, $c) = @_;
b7c570ac 438 my $model = $c->model( $self->{model_name} );
38017482 439 my $condition = $self->{model_search_condition} || {};
b7c570ac 440 my $attrs = $self->{model_search_attrs} || {};
38017482 441 $c->stash(rs => $model->search($condition, $attrs);
b7c570ac 442 }
38017482 443
444 sub load : Chained('base') PathPart('') CaptureArgs(1) {
445 my ($self, $c, $id) = @_;
446 my $model = $c->model( $self->{model_name} );
447 $c->stash(row => $model->find($id));
b7c570ac 448 }
38017482 449 1;
450
b7c570ac 451This example implements two simple actions. The C<list> action chains
452to a (currently non-existent) C<base> action and puts a result-set
453into the stash taking a configured C<model_name> as well as a search
454condition and attributes. This action is a
455L<chained|Catalyst::DispatchType::Chained> endpoint. The other action,
456called C< load > is a chain midpoint that takes one argument. It takes
457the value as an ID and loads the row from the configured model. Please
458not that the above code is simplified for clarity. It misses error
459handling, input validation, and probably other things.
38017482 460
b7c570ac 461The class above is not very useful on its own, but we can combine it
462with some custom actions by sub-classing it:
38017482 463
464 package MyApp::Controller::Foo;
465 use strict;
466 use base 'MyApp::Base::Controller::ModelBase';
467
b7c570ac 468 __PACKAGE__->config( model_name => 'DB::Foo',
469 model_search_condition=> { is_active => 1 },
470 model_search_attrs => { order_by => 'name' },
471 );
38017482 472
473 sub base : Chained PathPart('foo') CaptureArgs(0) { }
474
475 sub view : Chained('load') Args(0) {
476 my ($self, $c) = @_;
477 my $row = $c->stash->{row};
b7c570ac 478 $c->res->body(join ': ', $row->name,
479 $row->description); }
38017482 480 1;
481
b7c570ac 482This class uses the formerly created controller as a base
483class. First, we see the configurations that were used in the parent
484class. Next comes the C<base> action, where everything chains off of.
38017482 485
b7c570ac 486Note that inherited actions act like they were declared in your
487controller itself. You can therefor call them just by their name in
38017482 488C<forward>s, C<detaches> and C<Chained(..)> specifications. This is an
489important part of what makes this technique so useful.
490
b7c570ac 491The new C<view> action ties itself to the C<load> action specified in
492the base class and outputs the loaded row's C<name> and C<description>
493columns. The controller C<MyApp::Controller::Foo> now has these
494publicly available paths:
38017482 495
496=over
497
498=item /foo
499
b7c570ac 500Will call the controller's C<base>, then the base classes C<list>
501action.
38017482 502
503=item /foo/$id/view
504
b7c570ac 505First, the controller's C<base> will be called, then it will C<load>
506the row with the corresponding C<$id>. After that, C<view> will
507display some fields out of the object.
38017482 508
509=back
510
511=head2 Models and Views
512
b7c570ac 513If the functionality you'd like to add is really a data-set that you
514want to manipulate, for example internal document types, images,
515files, it might be better suited as a model.
38017482 516
b7c570ac 517The same applies for views. If your code handles representation or
518deals with the applications interface and should be universally
519available, it could be a perfect candidate for a view.
38017482 520
b7c570ac 521Please implement a C<process> method in your views. This method will
522be called by Catalyst if it is asked to forward to a component without
523a specified action. Note that C<process> is B<not a Catalyst action>
524but a simple Perl method.
38017482 525
526You are also encouraged to implement a C<render> method corresponding
527with the one in L<Catalyst::View::TT>. This has proven invaluable,
528because people can use your view for much more fine-grained content
529generation.
530
531Here is some example code for a fictional view:
532
533 package CatalystX::View::MyView;
534 use strict;
535 use base 'Catalyst::View';
536
537 sub process {
538 my ($self, $c) = @_;
38017482 539 my $template = $c->stash->{template};
b7c570ac 540 my $content = $self->render($c, $template, $c->stash);
38017482 541 $c->res->body( $content );
542 }
543
544 sub render {
545 my ($self, $c, $template, $args) = @_;
b7c570ac 546 # prepare content here
38017482 547 return $content;
548 }
38017482 549 1;
550
551=head2 Plugins
552
b7c570ac 553The first thing to say about plugins is that if you're not sure if
554your module should be a plugin, it probably shouldn't. It once was
555common to add features to Catalyst by writing plugins that provide
556accessors to said functionality. As Catalyst grew more popular, it
557became obvious that this qualifies as bad practice.
558
559By designing your module as a Catalyst plugin, every method you
560implement, import or inherit will be available via your applications
561context object. A plugin pollutes the global namespace, and you
562should be only doing that when you really need to.
563
564Often, developers design extensions as plugins because they need to
565get hold of the context object. Either to get at the stash or
566request/response objects are the widely spread reasons. It is,
567however, perfectly possible to implement a regular Catalyst component
568(read: model, view or controller) that receives the current context
569object via L<Catalyst::Component/"ACCEPT_CONTEXT($c, @args)">.
570
571When is a plugin suited to your task? Your code needs to be a
572plugin to act upon or alter specific parts of Catalyst's request
78170776 573lifecycle. If your functionality needs to change some C<prepare_*> or
b7c570ac 574C<finalize_*> stages, you won't get around a plugin.
575
78170776 576Note, if you just want to hook into such a stage, and run code before,
577or after it, then it is recommended that you use L<Moose>s method modifiers
578to do this.
579
b7c570ac 580Another valid target for a plugin architecture are things that
581B<really> have to be globally available, like sessions or
582authentication.
583
584B<Please do not> release Catalyst extensions as plugins only to
585provide some functionality application wide. Design it as a controller
586base class or another suiting technique with a smaller scope, so that
587your code only influences those parts of the application where it is
588needed, and namespace clashes and conflicts are ruled out.
38017482 589
590The implementation is pretty easy. Your plugin will be inserted in the
591application's inheritance list, above Catalyst itself. You can by this
b7c570ac 592alter Catalyst's request lifecycle behaviour. Every method you
593declare, every import in your package will be available as method on
594the application and the context object. As an example, let's say you
78170776 595want Catalyst to warn you every time uri_for was called without an action
596object as the first parameter, for example to test that all your chained
597uris are generated from actions (a recommended best practice).
598You could do this with this simple
38017482 599implementation (excuse the lame class name, it's just an example):
600
601 package Catalyst::Plugin::UriforUndefWarning;
602 use strict;
78170776 603 use Scalar::Util qw/blessed/;
20a4dd98 604 use MRO::Compat;
38017482 605
606 sub uri_for {
b7c570ac 607 my $c = shift;
38017482 608 my $uri = $c->next::method(@_);
78170776 609 $c->log->warn( 'uri_for with non action: ', join(', ', @_), )
610 if (!blessed($_[0]) || !$_[0]->isa('Catalyst::Action'));
38017482 611 return $uri;
612 }
613
614 1;
615
b7c570ac 616This would override Catalyst's C<uri_for> method and emit a C<warn>
78170776 617log entry containing the arguments to uri_for.
618
619Please note this is not a practical example, as string URLs are fine for
620static content etc.
621
622A simple example like this is actually better as a L<Moose> role, for example:
623
624 package CatalystX::UriforUndefWarning;
625 use Moose::Role;
626 use namespace::clean -except => 'meta';
627
628 after 'uri_for' => sub {
629 my ($c, $arg) = @_;
630 $c->log->warn( 'uri_for with non action: ', join(', ', @_), )
631 if (!blessed($_[0]) || !$_[0]->isa('Catalyst::Action'));
632 return $uri;
fa025310 633 };
38017482 634
635=head2 Factory components with COMPONENT()
636
b7c570ac 637Every component inheriting from L<Catalyst::Component> contains a
638C<COMPONENT> method. It is used on application startup by
639C<setup_components> to instantiate the component object for the
640Catalyst application. By default, this will merge the components own
641C<config>uration with the application wide overrides and call the
642class' C<new> method to return the component object.
38017482 643
b7c570ac 644You can override this method and do and return whatever you want.
fa025310 645However, you should use L<Class::C3> (via L<MRO::Compat>) to forward
646to the original C<COMPONENT> method to merge the configuration of
20a4dd98 647your component.
38017482 648
649Here is a stub C<COMPONENT> method:
650
651 package CatalystX::Component::Foo;
652 use strict;
653 use base 'Catalyst::Component';
654
20a4dd98 655 use MRO::Compat;
38017482 656
657 sub COMPONENT {
658 my $class = shift;
a70cede4 659 # Note: $app is like $c, but since the application isn't fully
660 # initialized, we don't want to call it $c yet. $config
661 # is a hashref of config options possibly set on this component.
662 my ($app, $config) = @_;
663
664 # Do things here before instantiation
665 $new = $class->next::method(@_);
666 # Do things to object after instantiation
667 return $new;
38017482 668 }
669
670The arguments are the class name of the component, the class name of
b7c570ac 671the application instantiating the component, and a hash reference with
672the controller's configuration.
38017482 673
b7c570ac 674You are free to re-bless the object, instantiate a whole other
675component or really do anything compatible with Catalyst's
676expectations on a component.
38017482 677
fa025310 678For more information, please see
679L<Catalyst::Component/"COMPONENT($c,$arguments)">.
38017482 680
681=head1 SEE ALSO
682
b7c570ac 683L<Catalyst>, L<Catalyst::Manual::Actions>, L<Catalyst::Component>
38017482 684
685=head1 AUTHOR
686
1972ebdd 687Robert Sedlacek C<< <rs@474.at> >>
688
689Jonathan Rockway C<< <jrockway@cpan.org> >>
38017482 690
691=head1 LICENSE AND COPYRIGHT
692
b7c570ac 693This document is free, you can redistribute it and/or modify it under
38017482 694the same terms as Perl itself.
695
696=cut
697