Plugin.pod formatting
[catagits/Catalyst-Runtime.git] / README
1 NAME
2     Catalyst - The Elegant MVC Web Application Framework
3
4 SYNOPSIS
5         # use the helper to start a new application
6         catalyst.pl MyApp
7
8         # add models, views, controllers
9         script/myapp_create.pl model Database DBIC dbi:SQLite:/path/to/db
10         script/myapp_create.pl view TT TT
11         script/myapp_create.pl controller Search
12
13         # built in testserver -- use -r to restart automatically on changes
14         script/myapp_server.pl
15
16         # command line testing interface
17         script/myapp_test.pl /yada
18
19         ### in MyApp.pm
20         use Catalyst qw/-Debug/; # include plugins here as well
21     
22         sub foo : Global { # called for /foo, /foo/1, /foo/1/2, etc.
23             my ( $self, $c, @args ) = @_; # args are qw/on you/ for /foo/on/you
24             $c->stash->{template} = 'foo.tt';
25             # lookup something from db -- stash vars are passed to TT
26             $c->stash->{data} = MyApp::Model::Database::Foo->search;
27             if ( $c->req->params->{bar} ) { # access GET or POST parameters
28                 $c->forward( 'bar' ); # process another action
29                 # do something else after forward returns            
30             }
31         }
32     
33         # The foo.tt TT template can easily use the stash data from the database
34         [% WHILE (item = data.next) %]
35             [% item.foo %]
36         [% END %]
37     
38         # called for /bar/of/soap, /bar/of/soap/10, etc.
39         sub bar : Path('/bar/of/soap') { ... }
40
41         # called for all actions, from the top-most controller inwards
42         sub auto : Private { 
43             my ( $self, $c ) = @_;
44             if ( !$c->user ) {
45                 $c->res->redirect( '/login' ); # require login
46                 return 0; # abort request and go immediately to end()
47             }
48             return 1;
49         }
50     
51         # called after the main action is finished
52         sub end : Private { 
53             my ( $self, $c ) = @_;
54             if ( scalar @{ $c->error } ) { ... } # handle errors
55             return if $c->res->body; # already have a response
56             $c->forward( 'MyApp::View::TT' ); # render template
57         }
58
59         ### in MyApp/Controller/Foo.pm
60         # called for /foo/bar
61         sub bar : Local { ... }
62     
63         # called for /blargle
64         sub blargle : Global { ... }
65     
66         # an index action matches /foo, but not /foo/1, etc.
67         sub index : Private { ... }
68     
69         ### in MyApp/Controller/Foo/Bar.pm
70         # called for /foo/bar/baz
71         sub baz : Local { ... }
72     
73         # first MyApp auto is called, then Foo auto, then this
74         sub auto : Private { ... }
75     
76         # powerful regular expression paths are also possible
77         sub details : Regex('^product/(\w+)/details$') {
78             my ( $self, $c ) = @_;
79             # extract the (\w+) from the URI
80             my $product = $c->req->snippets->[0];
81         }
82
83     See Catalyst::Manual::Intro for additional information.
84
85 DESCRIPTION
86     The key concept of Catalyst is DRY (Don't Repeat Yourself).
87
88     See Catalyst::Manual for more documentation.
89
90     Catalyst plugins can be loaded by naming them as arguments to the "use
91     Catalyst" statement. Omit the "Catalyst::Plugin::" prefix from the
92     plugin name, i.e., "Catalyst::Plugin::My::Module" becomes "My::Module".
93
94         use Catalyst qw/My::Module/;
95
96     Special flags like -Debug and -Engine can also be specified as arguments
97     when Catalyst is loaded:
98
99         use Catalyst qw/-Debug My::Module/;
100
101     The position of plugins and flags in the chain is important, because
102     they are loaded in exactly the order that they appear.
103
104     The following flags are supported:
105
106     -Debug
107         Enables debug output.
108
109     -Engine
110         Forces Catalyst to use a specific engine. Omit the
111         "Catalyst::Engine::" prefix of the engine name, i.e.:
112
113             use Catalyst qw/-Engine=CGI/;
114
115     -Home
116         Forces Catalyst to use a specific home directory.
117
118     -Log
119         Specifies log level.
120
121 METHODS
122   Information about the current request
123     $c->action
124         Returns a Catalyst::Action object for the current action, which
125         stringifies to the action name.
126
127     $c->namespace
128         Returns the namespace of the current action, i.e., the uri prefix
129         corresponding to the controller of the current action.
130
131     $c->request
132     $c->req
133         Returns the current Catalyst::Request object.
134
135   Processing and response to the current request
136     $c->forward( $action [, \@arguments ] )
137     $c->forward( $class, $method, [, \@arguments ] )
138         Forwards processing to a private action. If you give a class name
139         but no method, process() is called. You may also optionally pass
140         arguments in an arrayref. The action will receive the arguments in
141         @_ and $c->req->args. Upon returning from the function,
142         $c->req->args will be restored to the previous values.
143
144             $c->forward('/foo');
145             $c->forward('index');
146             $c->forward(qw/MyApp::Model::CDBI::Foo do_stuff/);
147             $c->forward('MyApp::View::TT');
148
149     $c->detach( $action [, \@arguments ] )
150     $c->detach( $class, $method, [, \@arguments ] )
151         The same as "forward", but doesn't return.
152
153     $c->error
154     $c->error($error, ...)
155     $c->error($arrayref)
156         Returns an arrayref containing error messages.
157
158             my @error = @{ $c->error };
159
160         Add a new error.
161
162             $c->error('Something bad happened');
163
164         Clear errors.
165
166             $c->error(0);
167
168     $c->response
169     $c->res
170         Returns the current Catalyst::Response object.
171
172     $c->stash
173         Returns a hashref to the stash, which may be used to store data and
174         pass it between components. You can also set hash keys by passing
175         arguments. The stash is automatically sent to the view.
176
177             $c->stash->{foo} = $bar;
178             $c->stash( { moose => 'majestic', qux => 0 } );
179             $c->stash( bar => 1, gorch => 2 ); # equivalent to passing a hashref
180     
181             # stash is automatically passed to the view for use in a template
182             $c->forward( 'MyApp::V::TT' );
183
184     $c->state
185         Contains the return value of the last executed action.
186
187   Component Accessors
188     $c->comp($name)
189     $c->component($name)
190         Gets a component object by name. This method is no longer
191         recommended. $c->controller, $c->model, and $c->view should be used
192         instead.
193
194     $c->controller($name)
195         Gets a Catalyst::Controller instance by name.
196
197             $c->controller('Foo')->do_stuff;
198
199     $c->model($name)
200         Gets a Catalyst::Model instance by name.
201
202             $c->model('Foo')->do_stuff;
203
204     $c->view($name)
205         Gets a Catalyst::View instance by name.
206
207             $c->view('Foo')->do_stuff;
208
209   Class data and helper classes
210     $c->config
211         Returns or takes a hashref containing the application's
212         configuration.
213
214             __PACKAGE__->config({ db => 'dsn:SQLite:foo.db' });
215
216     $c->debug
217         Overload to enable debug messages (same as -Debug option).
218
219     $c->dispatcher
220         Returns the dispatcher instance. Stringifies to class name.
221
222     $c->engine
223         Returns the engine instance. Stringifies to the class name.
224
225     $c->log
226         Returns the logging object instance. Unless it is already set,
227         Catalyst sets this up with a Catalyst::Log object. To use your own
228         log class:
229
230             $c->log( MyLogger->new );
231             $c->log->info( 'now logging with my own logger!' );
232
233         Your log class should implement the methods described in the
234         Catalyst::Log man page.
235
236   Utility methods
237     $c->path_to(@path)
238         Merges @path with $c->config->{home} and returns a Path::Class
239         object.
240
241         For example:
242
243             $c->path_to( 'db', 'sqlite.db' );
244
245     $c->plugin( $name, $class, @args )
246         Helper method for plugins. It creates a classdata accessor/mutator
247         and loads and instantiates the given class.
248
249             MyApp->plugin( 'prototype', 'HTML::Prototype' );
250
251             $c->prototype->define_javascript_functions;
252
253     MyApp->setup
254         Initializes the dispatcher and engine, loads any plugins, and loads
255         the model, view, and controller components. You may also specify an
256         array of plugins to load here, if you choose to not load them in the
257         "use Catalyst" line.
258
259             MyApp->setup;
260             MyApp->setup( qw/-Debug/ );
261
262     $c->uri_for( $path, [ @args ] )
263         Merges path with $c->request->base for absolute uri's and with
264         $c->request->match for relative uri's, then returns a normalized URI
265         object. If any args are passed, they are added at the end of the
266         path.
267
268     $c->welcome_message
269         Returns the Catalyst welcome HTML page.
270
271 INTERNAL METHODS
272     $c->benchmark( $coderef )
273         Takes a coderef with arguments and returns elapsed time as float.
274
275             my ( $elapsed, $status ) = $c->benchmark( sub { return 1 } );
276             $c->log->info( sprintf "Processing took %f seconds", $elapsed );
277
278     $c->components
279         Returns a hash of components.
280
281     $c->context_class
282         Returns or sets the context class.
283
284     $c->counter
285         Returns a hashref containing coderefs and execution counts (needed
286         for deep recursion detection).
287
288     $c->depth
289         Returns the number of actions on the current internal execution
290         stack.
291
292     $c->dispatch
293         Dispatches a request to actions.
294
295     $c->dispatcher_class
296         Returns or sets the dispatcher class.
297
298     $c->dump_these
299         Returns a list of 2-element array references (name, structure) pairs
300         that will be dumped on the error page in debug mode.
301
302     $c->engine_class
303         Returns or sets the engine class.
304
305     $c->execute( $class, $coderef )
306         Execute a coderef in given class and catch exceptions. Errors are
307         available via $c->error.
308
309     $c->finalize
310         Finalizes the request.
311
312     $c->finalize_body
313         Finalizes body.
314
315     $c->finalize_cookies
316         Finalizes cookies.
317
318     $c->finalize_error
319         Finalizes error.
320
321     $c->finalize_headers
322         Finalizes headers.
323
324     $c->finalize_output
325         An alias for finalize_body.
326
327     $c->finalize_read
328         Finalizes the input after reading is complete.
329
330     $c->finalize_uploads
331         Finalizes uploads. Cleans up any temporary files.
332
333     $c->get_action( $action, $namespace )
334         Gets an action in a given namespace.
335
336     $c->get_actions( $action, $namespace )
337         Gets all actions of a given name in a namespace and all parent
338         namespaces.
339
340     handle_request( $class, @arguments )
341         Called to handle each HTTP request.
342
343     $c->prepare( @arguments )
344         Creates a Catalyst context from an engine-specific request (Apache,
345         CGI, etc.).
346
347     $c->prepare_action
348         Prepares action.
349
350     $c->prepare_body
351         Prepares message body.
352
353     $c->prepare_body_chunk( $chunk )
354         Prepares a chunk of data before sending it to HTTP::Body.
355
356     $c->prepare_body_parameters
357         Prepares body parameters.
358
359     $c->prepare_connection
360         Prepares connection.
361
362     $c->prepare_cookies
363         Prepares cookies.
364
365     $c->prepare_headers
366         Prepares headers.
367
368     $c->prepare_parameters
369         Prepares parameters.
370
371     $c->prepare_path
372         Prepares path and base.
373
374     $c->prepare_query_parameters
375         Prepares query parameters.
376
377     $c->prepare_read
378         Prepares the input for reading.
379
380     $c->prepare_request
381         Prepares the engine request.
382
383     $c->prepare_uploads
384         Prepares uploads.
385
386     $c->prepare_write
387         Prepares the output for writing.
388
389     $c->request_class
390         Returns or sets the request class.
391
392     $c->response_class
393         Returns or sets the response class.
394
395     $c->read( [$maxlength] )
396         Reads a chunk of data from the request body. This method is designed
397         to be used in a while loop, reading $maxlength bytes on every call.
398         $maxlength defaults to the size of the request if not specified.
399
400         You have to set MyApp->config->{parse_on_demand} to use this
401         directly.
402
403     $c->run
404         Starts the engine.
405
406     $c->set_action( $action, $code, $namespace, $attrs )
407         Sets an action in a given namespace.
408
409     $c->setup_actions($component)
410         Sets up actions for a component.
411
412     $c->setup_components
413         Sets up components.
414
415     $c->setup_dispatcher
416     $c->setup_engine
417     $c->setup_home
418     $c->setup_log
419     $c->setup_plugins
420     $c->stack
421         Returns the stack.
422
423     $c->write( $data )
424         Writes $data to the output stream. When using this method directly,
425         you will need to manually set the Content-Length header to the
426         length of your output data, if known.
427
428     version
429         Returns the Catalyst version number. Mostly useful for "powered by"
430         messages in template systems.
431
432 INTERNAL ACTIONS
433     Catalyst uses internal actions like "_DISPATCH", "_BEGIN", "_AUTO"
434     "_ACTION" and "_END". These are by default not shown in the private
435     action table, but you can make them visible with a config parameter.
436
437         MyApp->config->{show_internal_actions} = 1;
438
439 CASE SENSITIVITY
440     By default Catalyst is not case sensitive, so "MyApp::C::FOO::Bar" is
441     mapped to "/foo/bar". You can activate case sensitivity with a config
442     parameter.
443
444         MyApp->config->{case_sensitive} = 1;
445
446     This causes "MyApp::C::Foo::Bar" to map to "/Foo/Bar".
447
448 ON-DEMAND PARSER
449     The request body is usually parsed at the beginning of a request, but if
450     you want to handle input yourself or speed things up a bit, you can
451     enable on-demand parsing with a config parameter.
452
453         MyApp->config->{parse_on_demand} = 1;
454     
455 PROXY SUPPORT
456     Many production servers operate using the common double-server approach,
457     with a lightweight frontend web server passing requests to a larger
458     backend server. An application running on the backend server must deal
459     with two problems: the remote user always appears to be 127.0.0.1 and
460     the server's hostname will appear to be "localhost" regardless of the
461     virtual host that the user connected through.
462
463     Catalyst will automatically detect this situation when you are running
464     the frontend and backend servers on the same machine. The following
465     changes are made to the request.
466
467         $c->req->address is set to the user's real IP address, as read from the
468         HTTP X-Forwarded-For header.
469     
470         The host value for $c->req->base and $c->req->uri is set to the real host,
471         as read from the HTTP X-Forwarded-Host header.
472
473     Obviously, your web server must support these headers for this to work.
474
475     In a more complex server farm environment where you may have your
476     frontend proxy server(s) on different machines, you will need to set a
477     configuration option to tell Catalyst to read the proxied data from the
478     headers.
479
480         MyApp->config->{using_frontend_proxy} = 1;
481     
482     If you do not wish to use the proxy support at all, you may set:
483
484         MyApp->config->{ignore_frontend_proxy} = 1;
485
486 THREAD SAFETY
487     Catalyst has been tested under Apache 2's threading mpm_worker,
488     mpm_winnt, and the standalone forking HTTP server on Windows. We believe
489     the Catalyst core to be thread-safe.
490
491     If you plan to operate in a threaded environment, remember that all
492     other modules you are using must also be thread-safe. Some modules, most
493     notably DBD::SQLite, are not thread-safe.
494
495 SUPPORT
496     IRC:
497
498         Join #catalyst on irc.perl.org.
499
500     Mailing Lists:
501
502         http://lists.rawmode.org/mailman/listinfo/catalyst
503         http://lists.rawmode.org/mailman/listinfo/catalyst-dev
504
505     Web:
506
507         http://catalyst.perl.org
508
509     Wiki:
510
511         http://dev.catalyst.perl.org
512
513 SEE ALSO
514     Catalyst::Manual - The Catalyst Manual
515     Catalyst::Component, Catalyst::Base - Base classes for components
516     Catalyst::Engine - Core Engine
517     Catalyst::Log - The Log Class.
518     Catalyst::Request - The Request Object
519     Catalyst::Response - The Response Object
520     Catalyst::Test - The test suite.
521
522 CREDITS
523     Andy Grundman
524
525     Andy Wardley
526
527     Andreas Marienborg
528
529     Andrew Bramble
530
531     Andrew Ford
532
533     Andrew Ruthven
534
535     Arthur Bergman
536
537     Autrijus Tang
538
539     Brian Cassidy
540
541     Christian Hansen
542
543     Christopher Hicks
544
545     Dan Sully
546
547     Danijel Milicevic
548
549     David Kamholz
550
551     David Naughton
552
553     Gary Ashton Jones
554
555     Geoff Richards
556
557     Jesse Sheidlower
558
559     Jesse Vincent
560
561     Jody Belka
562
563     Johan Lindstrom
564
565     Juan Camacho
566
567     Leon Brocard
568
569     Marcus Ramberg
570
571     Matt S Trout
572
573     Robert Sedlacek
574
575     Sam Vilain
576
577     Sascha Kiefer
578
579     Tatsuhiko Miyagawa
580
581     Ulf Edvinsson
582
583     Yuval Kogman
584
585 AUTHOR
586     Sebastian Riedel, "sri@oook.de"
587
588 LICENSE
589     This library is free software, you can redistribute it and/or modify it
590     under the same terms as Perl itself.
591