Plugin.pod formatting
[catagits/Catalyst-Runtime.git] / README
CommitLineData
fc7ec1d9 1NAME
2 Catalyst - The Elegant MVC Web Application Framework
3
4SYNOPSIS
5 # use the helper to start a new application
d01df17d 6 catalyst.pl MyApp
fc7ec1d9 7
8 # add models, views, controllers
b993339e 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
fc7ec1d9 12
b993339e 13 # built in testserver -- use -r to restart automatically on changes
f56990fa 14 script/myapp_server.pl
fc7ec1d9 15
b993339e 16 # command line testing interface
f56990fa 17 script/myapp_test.pl /yada
fc7ec1d9 18
b993339e 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') { ... }
4be6aea7 40
b993339e 41 # called for all actions, from the top-most controller inwards
42 sub auto : Private {
4be6aea7 43 my ( $self, $c ) = @_;
b993339e 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
4be6aea7 57 }
58
b993339e 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$') {
4be6aea7 78 my ( $self, $c ) = @_;
b993339e 79 # extract the (\w+) from the URI
80 my $product = $c->req->snippets->[0];
4be6aea7 81 }
fc7ec1d9 82
b993339e 83 See Catalyst::Manual::Intro for additional information.
92af75fc 84
fc7ec1d9 85DESCRIPTION
fc7ec1d9 86 The key concept of Catalyst is DRY (Don't Repeat Yourself).
87
88 See Catalyst::Manual for more documentation.
89
4be6aea7 90 Catalyst plugins can be loaded by naming them as arguments to the "use
91 Catalyst" statement. Omit the "Catalyst::Plugin::" prefix from the
b993339e 92 plugin name, i.e., "Catalyst::Plugin::My::Module" becomes "My::Module".
fc7ec1d9 93
b993339e 94 use Catalyst qw/My::Module/;
fc7ec1d9 95
01ba879f 96 Special flags like -Debug and -Engine can also be specified as arguments
4be6aea7 97 when Catalyst is loaded:
fc7ec1d9 98
99 use Catalyst qw/-Debug My::Module/;
100
101 The position of plugins and flags in the chain is important, because
4be6aea7 102 they are loaded in exactly the order that they appear.
fc7ec1d9 103
4be6aea7 104 The following flags are supported:
fc7ec1d9 105
4be6aea7 106 -Debug
b993339e 107 Enables debug output.
fbcc39ad 108
4be6aea7 109 -Engine
b993339e 110 Forces Catalyst to use a specific engine. Omit the
4be6aea7 111 "Catalyst::Engine::" prefix of the engine name, i.e.:
fc7ec1d9 112
b993339e 113 use Catalyst qw/-Engine=CGI/;
fc7ec1d9 114
fbcc39ad 115 -Home
b993339e 116 Forces Catalyst to use a specific home directory.
fbcc39ad 117
118 -Log
b993339e 119 Specifies log level.
fbcc39ad 120
4be6aea7 121METHODS
b993339e 122 Information about the current request
01ba879f 123 $c->action
b993339e 124 Returns a Catalyst::Action object for the current action, which
125 stringifies to the action name.
acd95255 126
b993339e 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.
fbcc39ad 130
b993339e 131 $c->request
132 $c->req
133 Returns the current Catalyst::Request object.
fbcc39ad 134
b993339e 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.
fbcc39ad 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
b993339e 149 $c->detach( $action [, \@arguments ] )
150 $c->detach( $class, $method, [, \@arguments ] )
151 The same as "forward", but doesn't return.
fbcc39ad 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
b993339e 164 Clear errors.
01ba879f 165
166 $c->error(0);
167
b993339e 168 $c->response
169 $c->res
170 Returns the current Catalyst::Response object.
4be6aea7 171
b993339e 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.
4be6aea7 176
b993339e 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' );
4be6aea7 183
b993339e 184 $c->state
185 Contains the return value of the last executed action.
fc7ec1d9 186
b993339e 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.
6f6e1bb4 193
b993339e 194 $c->controller($name)
195 Gets a Catalyst::Controller instance by name.
6f6e1bb4 196
b993339e 197 $c->controller('Foo')->do_stuff;
d1a31ac6 198
b993339e 199 $c->model($name)
200 Gets a Catalyst::Model instance by name.
fbcc39ad 201
b993339e 202 $c->model('Foo')->do_stuff;
fbcc39ad 203
b993339e 204 $c->view($name)
205 Gets a Catalyst::View instance by name.
206
207 $c->view('Foo')->do_stuff;
fbcc39ad 208
b993339e 209 Class data and helper classes
210 $c->config
211 Returns or takes a hashref containing the application's
212 configuration.
fbcc39ad 213
b993339e 214 __PACKAGE__->config({ db => 'dsn:SQLite:foo.db' });
fbcc39ad 215
b993339e 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:
fbcc39ad 229
b993339e 230 $c->log( MyLogger->new );
231 $c->log->info( 'now logging with my own logger!' );
fbcc39ad 232
b993339e 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.
01ba879f 240
241 For example:
242
b993339e 243 $c->path_to( 'db', 'sqlite.db' );
01ba879f 244
b993339e 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.
acd95255 248
b993339e 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.
acd95255 267
01ba879f 268 $c->welcome_message
269 Returns the Catalyst welcome HTML page.
270
fbcc39ad 271INTERNAL METHODS
b993339e 272 $c->benchmark( $coderef )
fbcc39ad 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
b993339e 279 Returns a hash of components.
fbcc39ad 280
b993339e 281 $c->context_class
282 Returns or sets the context class.
acd95255 283
fbcc39ad 284 $c->counter
b993339e 285 Returns a hashref containing coderefs and execution counts (needed
286 for deep recursion detection).
fbcc39ad 287
288 $c->depth
b993339e 289 Returns the number of actions on the current internal execution
290 stack.
fbcc39ad 291
292 $c->dispatch
b993339e 293 Dispatches a request to actions.
fbcc39ad 294
b993339e 295 $c->dispatcher_class
296 Returns or sets the dispatcher class.
acd95255 297
b993339e 298 $c->dump_these
60b53d07 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
b993339e 302 $c->engine_class
303 Returns or sets the engine class.
acd95255 304
b993339e 305 $c->execute( $class, $coderef )
fbcc39ad 306 Execute a coderef in given class and catch exceptions. Errors are
307 available via $c->error.
308
309 $c->finalize
b993339e 310 Finalizes the request.
fbcc39ad 311
312 $c->finalize_body
b993339e 313 Finalizes body.
fbcc39ad 314
315 $c->finalize_cookies
b993339e 316 Finalizes cookies.
fbcc39ad 317
318 $c->finalize_error
b993339e 319 Finalizes error.
fbcc39ad 320
321 $c->finalize_headers
b993339e 322 Finalizes headers.
fbcc39ad 323
324 $c->finalize_output
325 An alias for finalize_body.
326
327 $c->finalize_read
b993339e 328 Finalizes the input after reading is complete.
fbcc39ad 329
330 $c->finalize_uploads
b993339e 331 Finalizes uploads. Cleans up any temporary files.
fbcc39ad 332
60b53d07 333 $c->get_action( $action, $namespace )
b993339e 334 Gets an action in a given namespace.
fbcc39ad 335
60b53d07 336 $c->get_actions( $action, $namespace )
b993339e 337 Gets all actions of a given name in a namespace and all parent
60b53d07 338 namespaces.
339
fbcc39ad 340 handle_request( $class, @arguments )
b993339e 341 Called to handle each HTTP request.
fbcc39ad 342
b993339e 343 $c->prepare( @arguments )
344 Creates a Catalyst context from an engine-specific request (Apache,
345 CGI, etc.).
fbcc39ad 346
347 $c->prepare_action
b993339e 348 Prepares action.
fbcc39ad 349
350 $c->prepare_body
b993339e 351 Prepares message body.
fbcc39ad 352
01ba879f 353 $c->prepare_body_chunk( $chunk )
b993339e 354 Prepares a chunk of data before sending it to HTTP::Body.
01ba879f 355
fbcc39ad 356 $c->prepare_body_parameters
b993339e 357 Prepares body parameters.
fbcc39ad 358
359 $c->prepare_connection
b993339e 360 Prepares connection.
fbcc39ad 361
362 $c->prepare_cookies
b993339e 363 Prepares cookies.
fbcc39ad 364
365 $c->prepare_headers
b993339e 366 Prepares headers.
fbcc39ad 367
368 $c->prepare_parameters
b993339e 369 Prepares parameters.
fbcc39ad 370
371 $c->prepare_path
b993339e 372 Prepares path and base.
fbcc39ad 373
374 $c->prepare_query_parameters
b993339e 375 Prepares query parameters.
fbcc39ad 376
377 $c->prepare_read
b993339e 378 Prepares the input for reading.
fbcc39ad 379
380 $c->prepare_request
b993339e 381 Prepares the engine request.
fbcc39ad 382
383 $c->prepare_uploads
b993339e 384 Prepares uploads.
fbcc39ad 385
386 $c->prepare_write
b993339e 387 Prepares the output for writing.
fbcc39ad 388
b993339e 389 $c->request_class
390 Returns or sets the request class.
acd95255 391
b993339e 392 $c->response_class
393 Returns or sets the response class.
acd95255 394
fbcc39ad 395 $c->read( [$maxlength] )
b993339e 396 Reads a chunk of data from the request body. This method is designed
fbcc39ad 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 )
b993339e 407 Sets an action in a given namespace.
fbcc39ad 408
409 $c->setup_actions($component)
b993339e 410 Sets up actions for a component.
fbcc39ad 411
412 $c->setup_components
b993339e 413 Sets up components.
fbcc39ad 414
415 $c->setup_dispatcher
416 $c->setup_engine
417 $c->setup_home
418 $c->setup_log
419 $c->setup_plugins
acd95255 420 $c->stack
b993339e 421 Returns the stack.
acd95255 422
fbcc39ad 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
01ba879f 428 version
b993339e 429 Returns the Catalyst version number. Mostly useful for "powered by"
01ba879f 430 messages in template systems.
431
60b53d07 432INTERNAL ACTIONS
433 Catalyst uses internal actions like "_DISPATCH", "_BEGIN", "_AUTO"
b993339e 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.
60b53d07 436
437 MyApp->config->{show_internal_actions} = 1;
438
f56990fa 439CASE SENSITIVITY
b993339e 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.
f56990fa 443
444 MyApp->config->{case_sensitive} = 1;
445
b993339e 446 This causes "MyApp::C::Foo::Bar" to map to "/Foo/Bar".
fbcc39ad 447
448ON-DEMAND PARSER
449 The request body is usually parsed at the beginning of a request, but if
b993339e 450 you want to handle input yourself or speed things up a bit, you can
fbcc39ad 451 enable on-demand parsing with a config parameter.
452
453 MyApp->config->{parse_on_demand} = 1;
454
455PROXY 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
b993339e 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.
fbcc39ad 462
463 Catalyst will automatically detect this situation when you are running
b993339e 464 the frontend and backend servers on the same machine. The following
fbcc39ad 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
b993339e 468 HTTP X-Forwarded-For header.
fbcc39ad 469
470 The host value for $c->req->base and $c->req->uri is set to the real host,
b993339e 471 as read from the HTTP X-Forwarded-Host header.
fbcc39ad 472
b993339e 473 Obviously, your web server must support these headers for this to work.
fbcc39ad 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
486THREAD 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.
d1a31ac6 494
92af75fc 495SUPPORT
496 IRC:
497
498 Join #catalyst on irc.perl.org.
499
b993339e 500 Mailing Lists:
92af75fc 501
502 http://lists.rawmode.org/mailman/listinfo/catalyst
503 http://lists.rawmode.org/mailman/listinfo/catalyst-dev
4be6aea7 504
6f6e1bb4 505 Web:
506
507 http://catalyst.perl.org
508
b993339e 509 Wiki:
510
511 http://dev.catalyst.perl.org
512
fc7ec1d9 513SEE ALSO
4be6aea7 514 Catalyst::Manual - The Catalyst Manual
b993339e 515 Catalyst::Component, Catalyst::Base - Base classes for components
4be6aea7 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.
fc7ec1d9 521
4e449be9 522CREDITS
523 Andy Grundman
524
fbcc39ad 525 Andy Wardley
526
01ba879f 527 Andreas Marienborg
528
529 Andrew Bramble
530
4e449be9 531 Andrew Ford
532
533 Andrew Ruthven
534
fbcc39ad 535 Arthur Bergman
536
4e449be9 537 Autrijus Tang
538
acd95255 539 Brian Cassidy
540
4e449be9 541 Christian Hansen
542
543 Christopher Hicks
544
545 Dan Sully
546
547 Danijel Milicevic
548
b993339e 549 David Kamholz
550
4e449be9 551 David Naughton
552
553 Gary Ashton Jones
554
555 Geoff Richards
556
557 Jesse Sheidlower
558
fbcc39ad 559 Jesse Vincent
560
4e449be9 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
60b53d07 575 Sam Vilain
576
acd95255 577 Sascha Kiefer
578
4e449be9 579 Tatsuhiko Miyagawa
580
581 Ulf Edvinsson
582
01ba879f 583 Yuval Kogman
584
fc7ec1d9 585AUTHOR
586 Sebastian Riedel, "sri@oook.de"
587
fc7ec1d9 588LICENSE
01ba879f 589 This library is free software, you can redistribute it and/or modify it
590 under the same terms as Perl itself.
fc7ec1d9 591