docs for reusable actions
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Manual / Actions.pod
1 =head1 NAME
2
3 Catalyst::Manual::Actions - Catalyst Reusable Actions 
4
5 =head1 DESCRIPTION
6
7 This section of the manual describes the reusable action system in catalyst,
8 how they work, descriptions of some existing ones, and how to write your own.
9 Reusable actions are attributes on Catalyst methods that allow you to decorate
10 your method with functions running before or after the method call.
11 This can be used to implement commonly used action patterns, while still
12 leaving you full freedom to customize them.
13
14 =head1 USING ACTIONS
15
16 This is pretty simple. It works just like the normal dispatch attributes you
17 are used to, like Local or Private:
18
19   sub Hello : Local Action('SayBefore') { 
20         $c->res->output( 'Hello '.$c->stash->{what} );
21   }
22
23 In this example, we expect the SayBefore action to magically populate stash with
24 something relevant before Hello is run.  In the next section we'll show you
25 how to implement it. If you want it in another namespace than Catalyst::Action
26 you can prefix the action name with a '+', for instance '+Foo::SayBefore',
27 or if you just want it under your application namespace instead, use MyAction,
28 like MyAction('SayBefore').
29
30 =head1 WRITING YOUR OWN ACTIONS
31
32 Implementing the action is self is almost as easy. Just use L<Catalyst::Action>
33 as a base class and decorate the 'execute' call in the Action class:
34
35   package Catalyst::Action::SayBefore;
36
37   use base 'Catalyst::Action';
38
39   sub execute {
40     my $self = shift;
41     my ( $controller, $c, $test ) = @_;
42     $c->stash->{what} = 'world';
43     $self->NEXT::execute( @_ );
44   };
45
46   1;
47
48 If you want to do something after the action, just put it after the execute 
49 call. Pretty simple, huh?
50
51 =head1 ACTIONS
52
53 =head2 Catalyst::Action::RenderView
54
55 This is meant to decorate end actions. It's similar in operation to 
56 L<Catalyst::Plugin::DefaultEnd>, but allows you to decide on an action
57 level rather than on an application level where it should be run.
58
59 =cut