Merge
[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
4d719c7e 123class, and shipped to CPAN.
124Please see L<Catalyst::Manual::CatalystAndMoose> for specific information
125about using Roles in combination with Catalyst, and L<Moose::Manual::Roles>
126for more information about roles in general.
78170776 127
38017482 128=head2 Inheritance and overriding methods
129
23cf3a36 130When overriding a method, keep in mind that some day additionall
38017482 131arguments may be provided to the method, if the last parameter is not
132a flat list. It is thus better to override a method by shifting the
133invocant off of C<@_> and assign the rest of the used arguments, so
134you can pass your complete arguments to the original method via C<@_>:
135
20a4dd98 136 use MRO::Compat; ...
38017482 137
fa025310 138 sub foo {
139 my $self = shift;
140 my ($bar, $baz) = @_; # ... return
141 $self->next::method(@_);
142 }
38017482 143
144If you would do the common
145
146 my ($self, $foo, $bar) = @_;
147
148you'd have to use a much uglier construct to ensure that all arguments
149will be passed along and the method is future proof:
150
151 $self->next::method(@_[ 1 .. $#_ ]);
152
153=head2 Tests and documentation
154
b7c570ac 155When you release your module to the CPAN, proper documentation and at
156least a basic test suite (which means more than pod or even just
157C<use_ok>, sorry) gives people a good base to contribute to the
158module. It also shows that you care for your users. If you would like
159your module to become a recommended addition, these things will prove
38017482 160invaluable.
161
1972ebdd 162If you're just getting started, try using
163L<CatalystX::Starter|CatalystX::Starter> to generate some example
164tests for your module.
165
38017482 166=head2 Maintenance
167
b7c570ac 168In planning to release a module to the community (Catalyst or CPAN and
169Perl), you should consider if you have the resources to keep it up to
170date, including fixing bugs and accepting contributions.
38017482 171
b7c570ac 172If you're not sure about this, you can always ask in the proper
173Catalyst or Perl channels if someone else might be interested in the
174project, and would jump in as co-maintainer.
38017482 175
b7c570ac 176A public repository can further ease interaction with the
177community. Even read only access enables people to provide you with
178patches to your current development version. subversion, SVN and SVK,
179are broadly preferred in the Catalyst community.
38017482 180
b7c570ac 181If you're developing a Catalyst extension, please consider asking the
182core team for space in Catalyst's own subversion repository. You can
183get in touch about this via IRC or the Catalyst developers mailing
184list.
38017482 185
186=head2 The context object
187
188Sometimes you want to get a hold of the context object in a component
b7c570ac 189that was created on startup time, where no context existed yet. Often
38017482 190this is about the model reading something out of the stash or other
b7c570ac 191context information (current language, for example).
38017482 192
b7c570ac 193If you use the context object in your component you have tied it to an
194existing request. This means that you might get into problems when
195you try to use the component (e.g. the model - the most common case)
196outside of Catalyst, for example in cronjobs.
38017482 197
b7c570ac 198A stable solution to this problem is to design the Catalyst model
199separately from the underlying model logic. Let's take
200L<Catalyst::Model::DBIC::Schema> as an example. You can create a
38017482 201schema outside of Catalyst that knows nothing about the web. This kind
202of design ensures encapsulation and makes development and maintenance
203a whole lot easier. The you use the aforementioned model to tie your
b7c570ac 204schema to your application. This gives you a C<MyApp::DBIC> (the name
205is of course just an example) model as well as
206C<MyApp::DBIC::TableName> models to access your result sources
207directly.
208
209By creating such a thin layer between the actual model and the
210Catalyst application, the schema itself is not at all tied to any
211application and the layer in-between can access the model's API using
212information from the context object.
213
214A Catalyst component accesses the context object at request time with
38017482 215L<Catalyst::Component/"ACCEPT_CONTEXT($c, @args)">.
216
217=head1 CONFIGURATION
218
b7c570ac 219The application has to interact with the extension with some
220configuration. There is of course again more than one way to do it.
38017482 221
222=head2 Attributes
223
b7c570ac 224You can specify any valid Perl attribute on Catalyst actions you like.
225(See L<attributes/"Syntax of Attribute Lists"> for a description of
226what is valid.) These will be available on the C<Catalyst::Action>
227instance via its C<attributes> accessor. To give an example, this
228action:
38017482 229
230 sub foo : Local Bar('Baz') {
231 my ($self, $c) = @_;
bbddff00 232 my $attributes = $self->action_for('foo')->attributes;
b7c570ac 233 $c->res->body($attributes->{Bar}[0] );
38017482 234 }
235
b7c570ac 236will set the response body to C<Baz>. The values always come in an
237array reference. As you can see, you can use attributes to configure
238your actions. You can specify or alter these attributes via
239L</"Component Configuration">, or even react on them as soon as
240Catalyst encounters them by providing your own L<component base
241class|/"Component Base Classes">.
38017482 242
243=head2 Creating custom accessors
244
b7c570ac 245L<Catalyst::Component> uses L<Class::Accessor::Fast> for accessor
246creation. Please refer to the modules documentation for usage
38017482 247information.
248
249=head2 Component configuration
250
b7c570ac 251At creation time, the class configuration of your component (the one
252available via C<$self-E<gt>config>) will be merged with possible
38017482 253configuration settings from the applications configuration (either
b7c570ac 254directly or via config file). This is then stored in the controller
255object's hash reference. So, if you read possible configurations like:
38017482 256
257 my $model_name = $controller->{model_name};
258
b7c570ac 259you will get the right value. The C<config> accessor always only
38017482 260contains the original class configuration and must not be used for
261component configuration.
262
263You are advised to create accessors on your component class for your
264configuration values. This is good practice and makes it easier to
bbddff00 265capture configuration key typos, or missing keys.
266
267You can do this with L<Moose>:
268
269 package MyApp::Controller::Foo;
270 use Moose;
271 use namespace::autoclean;
272 BEGIN { extends 'Catalyst::Controller' };
273
274 has model_name ( is => 'ro', required => 1 );
38017482 275
38017482 276 ...
277 my $model_name = $controller->model_name;
278
279=head1 IMPLEMENTATION
280
b7c570ac 281This part contains the technical details of various implementation
38017482 282methods. Please read the L</"BEST PRACTICES"> before you start your
283implementation, if you haven't already.
284
285=head2 Action classes
286
287Usually, your action objects are of the class L<Catalyst::Action>.
288You can override this with the C<ActionClass> attribute to influence
b7c570ac 289execution and/or dispatching of the action. A widely used example of
290this is L<Catalyst::Action::RenderView>, which is used in every newly
291created Catalyst application in your root controller:
38017482 292
293 sub end : ActionClass('RenderView') { }
294
b7c570ac 295Usually, you want to override the C<execute> and/or the C<match>
296method. The execute method of the action will naturally call the
297methods code. You can surround this by overriding the method in a
298subclass:
38017482 299
20a4dd98 300 package Catalyst::Action::MyFoo;
bbddff00 301 use Moose;
302 use namespace::autoclean;
20a4dd98 303 use MRO::Compat;
bbddff00 304 extends 'Catalyst::Action';
38017482 305
306 sub execute {
307 my $self = shift;
308 my ($controller, $c, @args) = @_;
38017482 309 # put your 'before' code here
310 my $r = $self->next::method(@_);
311 # put your 'after' code here
38017482 312 return $r;
313 }
38017482 314 1;
315
20a4dd98 316We are using L<MRO::Compat> to ensure that you have the next::method
317call, from L<Class::C3> (in older perls), or natively (if you are using
318perl 5.10) to re-dispatch to the original C<execute> method in the
319L<Catalyst::Action> class.
38017482 320
b7c570ac 321The Catalyst dispatcher handles an incoming request and, depending
322upon the dispatch type, will call the appropriate target or chain.
323From time to time it asks the actions themselves, or through the
324controller, if they would match the current request. That's what the
325C<match> method does. So by overriding this, you can change on what
326the action will match and add new matching criteria.
38017482 327
b7c570ac 328For example, the action class below will make the action only match on
329Mondays:
38017482 330
78170776 331 package Catalyst::Action::OnlyMondays;
bbddff00 332 use Moose;
333 use namespace::autoclean;
20a4dd98 334 use MRO::Compat;
bbddff00 335 extends 'Catalyst::Action';
38017482 336
337 sub match {
338 my $self = shift;
339 return 0 if ( localtime(time) )[6] == 1;
340 return $self->next::method(@_);
b7c570ac 341 }
38017482 342 1;
343
344And this is how we'd use it:
345
346 sub foo: Local ActionClass('OnlyMondays') {
347 my ($self, $c) = @_;
348 $c->res->body('I feel motivated!');
349 }
350
b7c570ac 351If you are using action classes often or have some specific base
352classes that you want to specify more conveniently, you can implement
353a component base class providing an attribute handler.
38017482 354
bbddff00 355It is not possible to use multiple action classes at once, however
356L<Catalyst::Controller::ActionRole> allows you to apply L<Moose Roles|Moose::Role>
357to actions.
358
359For further information on action classes and roles, please refer to
38017482 360L<Catalyst::Action> and L<Catalyst::Manual::Actions>.
361
362=head2 Component base classes
363
b7c570ac 364Many L<Catalyst::Plugin> that were written in Catalyst's early days
365should really have been just controller base classes. With such a
366class, you could provide functionality scoped to a single controller,
367not polluting the global namespace in the context object.
38017482 368
b7c570ac 369You can provide regular Perl methods in a base class as well as
370actions which will be inherited to the subclass. Please refer to
371L</Controllers> for an example of this.
38017482 372
b7c570ac 373You can introduce your own attributes by specifying a handler method
374in the controller base. For example, to use a C<FullClass> attribute
375to specify a fully qualified action class name, you could use the
376following implementation. Note, however, that this functionality is
377already provided via the C<+> prefix for action classes. A simple
38017482 378
379 sub foo : Local ActionClass('+MyApp::Action::Bar') { ... }
380
381will use C<MyApp::Action::Bar> as action class.
382
bbddff00 383 package MyApp::Base::Controller::FullClass;
384 use Moose;
385 use namespace::autoclean;
386 BEGIN { extends 'Catalyst::Controller'; }
38017482 387
388 sub _parse_FullClass_attr {
389 my ($self, $app_class, $action_name, $value, $attrs) = @_;
390 return( ActionClass => $value );
391 }
38017482 392 1;
393
b7c570ac 394Note that the full line of arguments is only provided for completeness
395sake. We could use this attribute in a subclass like any other
396Catalyst attribute:
38017482 397
398 package MyApp::Controller::Foo;
bbddff00 399 use Moose;
400 use namespace::autoclean;
401 BEGIN { extends 'MyApp::Base::Controller::FullClass'; }
38017482 402
403 sub foo : Local FullClass('MyApp::Action::Bar') { ... }
404
405 1;
406
407=head2 Controllers
408
b7c570ac 409Many things can happen in controllers, and it often improves
410maintainability to abstract some of the code out into reusable base
38017482 411classes.
412
413You can provide usual Perl methods that will be available via your
b7c570ac 414controller object, or you can even define Catalyst actions which will
415be inherited by the subclasses. Consider this controller base class:
38017482 416
417 package MyApp::Base::Controller::ModelBase;
bbddff00 418 use Moose;
419 use namespace::autoclean;
420
421 BEGIN { extends 'Catalyst::Controller'; }
38017482 422
423 sub list : Chained('base') PathPart('') Args(0) {
424 my ($self, $c) = @_;
b7c570ac 425 my $model = $c->model( $self->{model_name} );
38017482 426 my $condition = $self->{model_search_condition} || {};
b7c570ac 427 my $attrs = $self->{model_search_attrs} || {};
38017482 428 $c->stash(rs => $model->search($condition, $attrs);
bbddff00 429 }
38017482 430
431 sub load : Chained('base') PathPart('') CaptureArgs(1) {
432 my ($self, $c, $id) = @_;
433 my $model = $c->model( $self->{model_name} );
434 $c->stash(row => $model->find($id));
bbddff00 435 }
38017482 436 1;
437
b7c570ac 438This example implements two simple actions. The C<list> action chains
439to a (currently non-existent) C<base> action and puts a result-set
440into the stash taking a configured C<model_name> as well as a search
441condition and attributes. This action is a
442L<chained|Catalyst::DispatchType::Chained> endpoint. The other action,
443called C< load > is a chain midpoint that takes one argument. It takes
444the value as an ID and loads the row from the configured model. Please
445not that the above code is simplified for clarity. It misses error
446handling, input validation, and probably other things.
38017482 447
b7c570ac 448The class above is not very useful on its own, but we can combine it
449with some custom actions by sub-classing it:
38017482 450
451 package MyApp::Controller::Foo;
bbddff00 452 use Moose;
453 use namespace::autoclean;
454
455 BEGIN { extends 'MyApp::Base::Controller::ModelBase'; }
38017482 456
b7c570ac 457 __PACKAGE__->config( model_name => 'DB::Foo',
458 model_search_condition=> { is_active => 1 },
459 model_search_attrs => { order_by => 'name' },
460 );
38017482 461
462 sub base : Chained PathPart('foo') CaptureArgs(0) { }
463
464 sub view : Chained('load') Args(0) {
465 my ($self, $c) = @_;
466 my $row = $c->stash->{row};
b7c570ac 467 $c->res->body(join ': ', $row->name,
468 $row->description); }
38017482 469 1;
470
b7c570ac 471This class uses the formerly created controller as a base
472class. First, we see the configurations that were used in the parent
473class. Next comes the C<base> action, where everything chains off of.
38017482 474
b7c570ac 475Note that inherited actions act like they were declared in your
476controller itself. You can therefor call them just by their name in
38017482 477C<forward>s, C<detaches> and C<Chained(..)> specifications. This is an
478important part of what makes this technique so useful.
479
b7c570ac 480The new C<view> action ties itself to the C<load> action specified in
481the base class and outputs the loaded row's C<name> and C<description>
482columns. The controller C<MyApp::Controller::Foo> now has these
483publicly available paths:
38017482 484
485=over
486
487=item /foo
488
b7c570ac 489Will call the controller's C<base>, then the base classes C<list>
490action.
38017482 491
492=item /foo/$id/view
493
b7c570ac 494First, the controller's C<base> will be called, then it will C<load>
495the row with the corresponding C<$id>. After that, C<view> will
496display some fields out of the object.
38017482 497
498=back
499
500=head2 Models and Views
501
b7c570ac 502If the functionality you'd like to add is really a data-set that you
503want to manipulate, for example internal document types, images,
504files, it might be better suited as a model.
38017482 505
b7c570ac 506The same applies for views. If your code handles representation or
507deals with the applications interface and should be universally
508available, it could be a perfect candidate for a view.
38017482 509
b7c570ac 510Please implement a C<process> method in your views. This method will
511be called by Catalyst if it is asked to forward to a component without
512a specified action. Note that C<process> is B<not a Catalyst action>
513but a simple Perl method.
38017482 514
515You are also encouraged to implement a C<render> method corresponding
516with the one in L<Catalyst::View::TT>. This has proven invaluable,
517because people can use your view for much more fine-grained content
518generation.
519
520Here is some example code for a fictional view:
521
bbddff00 522 package Catalyst::View::MyView;
523 use Moose;
524 use namespace::autoclean;
525
526 extends 'Catalyst::View';
38017482 527
528 sub process {
529 my ($self, $c) = @_;
38017482 530 my $template = $c->stash->{template};
b7c570ac 531 my $content = $self->render($c, $template, $c->stash);
38017482 532 $c->res->body( $content );
533 }
534
535 sub render {
536 my ($self, $c, $template, $args) = @_;
b7c570ac 537 # prepare content here
38017482 538 return $content;
539 }
38017482 540 1;
541
542=head2 Plugins
543
b7c570ac 544The first thing to say about plugins is that if you're not sure if
545your module should be a plugin, it probably shouldn't. It once was
546common to add features to Catalyst by writing plugins that provide
547accessors to said functionality. As Catalyst grew more popular, it
548became obvious that this qualifies as bad practice.
549
550By designing your module as a Catalyst plugin, every method you
551implement, import or inherit will be available via your applications
552context object. A plugin pollutes the global namespace, and you
553should be only doing that when you really need to.
554
555Often, developers design extensions as plugins because they need to
556get hold of the context object. Either to get at the stash or
557request/response objects are the widely spread reasons. It is,
558however, perfectly possible to implement a regular Catalyst component
559(read: model, view or controller) that receives the current context
560object via L<Catalyst::Component/"ACCEPT_CONTEXT($c, @args)">.
561
562When is a plugin suited to your task? Your code needs to be a
563plugin to act upon or alter specific parts of Catalyst's request
78170776 564lifecycle. If your functionality needs to change some C<prepare_*> or
b7c570ac 565C<finalize_*> stages, you won't get around a plugin.
566
78170776 567Note, if you just want to hook into such a stage, and run code before,
568or after it, then it is recommended that you use L<Moose>s method modifiers
569to do this.
570
b7c570ac 571Another valid target for a plugin architecture are things that
572B<really> have to be globally available, like sessions or
573authentication.
574
575B<Please do not> release Catalyst extensions as plugins only to
576provide some functionality application wide. Design it as a controller
bbddff00 577base class or another better suited technique with a smaller scope, so that
b7c570ac 578your code only influences those parts of the application where it is
579needed, and namespace clashes and conflicts are ruled out.
38017482 580
581The implementation is pretty easy. Your plugin will be inserted in the
582application's inheritance list, above Catalyst itself. You can by this
b7c570ac 583alter Catalyst's request lifecycle behaviour. Every method you
584declare, every import in your package will be available as method on
585the application and the context object. As an example, let's say you
78170776 586want Catalyst to warn you every time uri_for was called without an action
587object as the first parameter, for example to test that all your chained
588uris are generated from actions (a recommended best practice).
589You could do this with this simple
38017482 590implementation (excuse the lame class name, it's just an example):
591
592 package Catalyst::Plugin::UriforUndefWarning;
593 use strict;
78170776 594 use Scalar::Util qw/blessed/;
20a4dd98 595 use MRO::Compat;
38017482 596
597 sub uri_for {
b7c570ac 598 my $c = shift;
38017482 599 my $uri = $c->next::method(@_);
78170776 600 $c->log->warn( 'uri_for with non action: ', join(', ', @_), )
601 if (!blessed($_[0]) || !$_[0]->isa('Catalyst::Action'));
38017482 602 return $uri;
603 }
604
605 1;
606
b7c570ac 607This would override Catalyst's C<uri_for> method and emit a C<warn>
78170776 608log entry containing the arguments to uri_for.
609
610Please note this is not a practical example, as string URLs are fine for
611static content etc.
612
613A simple example like this is actually better as a L<Moose> role, for example:
614
615 package CatalystX::UriforUndefWarning;
616 use Moose::Role;
4d719c7e 617 use namespace::autoclean;
78170776 618
619 after 'uri_for' => sub {
620 my ($c, $arg) = @_;
621 $c->log->warn( 'uri_for with non action: ', join(', ', @_), )
622 if (!blessed($_[0]) || !$_[0]->isa('Catalyst::Action'));
623 return $uri;
fa025310 624 };
bbddff00 625
626Note that Catalyst will load any Moose Roles in the plugin list,
627and apply them to your application class.
38017482 628
629=head2 Factory components with COMPONENT()
630
b7c570ac 631Every component inheriting from L<Catalyst::Component> contains a
632C<COMPONENT> method. It is used on application startup by
633C<setup_components> to instantiate the component object for the
634Catalyst application. By default, this will merge the components own
635C<config>uration with the application wide overrides and call the
636class' C<new> method to return the component object.
38017482 637
b7c570ac 638You can override this method and do and return whatever you want.
fa025310 639However, you should use L<Class::C3> (via L<MRO::Compat>) to forward
640to the original C<COMPONENT> method to merge the configuration of
20a4dd98 641your component.
38017482 642
643Here is a stub C<COMPONENT> method:
644
645 package CatalystX::Component::Foo;
bbddff00 646 use Moose;
647 use namespace::autoclean;
648
649 extends 'Catalyst::Component';
38017482 650
651 sub COMPONENT {
652 my $class = shift;
a70cede4 653 # Note: $app is like $c, but since the application isn't fully
654 # initialized, we don't want to call it $c yet. $config
655 # is a hashref of config options possibly set on this component.
656 my ($app, $config) = @_;
657
658 # Do things here before instantiation
659 $new = $class->next::method(@_);
660 # Do things to object after instantiation
661 return $new;
38017482 662 }
663
664The arguments are the class name of the component, the class name of
b7c570ac 665the application instantiating the component, and a hash reference with
666the controller's configuration.
38017482 667
b7c570ac 668You are free to re-bless the object, instantiate a whole other
669component or really do anything compatible with Catalyst's
670expectations on a component.
38017482 671
fa025310 672For more information, please see
673L<Catalyst::Component/"COMPONENT($c,$arguments)">.
38017482 674
bbddff00 675=head2 Applying roles to parts of the framework
676
677L<CatalystX::RoleApplicator> will allow you to apply Roles to
678the following classes:
679
680=over
681
682=item Request
683
684=item Response
685
686=item Engine
687
688=item Dispatcher
689
690=item Stats
691
692=back
693
694These roles can add new methods to these classes, or wrap preexisting methods.
695
696The namespace for roles like this is C<Catalyst::TraitFor::XXX::YYYY>.
697
698For an example of a CPAN component implemented in this manor, see
699L<Catalyst::TraitFor::Request::BrowserDetect>.
700
38017482 701=head1 SEE ALSO
702
b7c570ac 703L<Catalyst>, L<Catalyst::Manual::Actions>, L<Catalyst::Component>
38017482 704
bbddff00 705=head1 AUTHORS
38017482 706
bbddff00 707Catalyst Contributors, see Catalyst.pm
1972ebdd 708
bbddff00 709=head1 COPYRIGHT
38017482 710
bbddff00 711This library is free software. You can redistribute it and/or modify it under
38017482 712the same terms as Perl itself.
713
714=cut
715
bbddff00 716