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