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