1Catalyst::Manual::ActioUnsse(r3)Contributed Perl DocumenCtaattailoynst::Manual::Actions(3)
2
3
4
6 Catalyst::Manual::Actions - Catalyst Reusable Actions
7
9 This section of the manual describes the reusable action system in
10 Catalyst, how such actions work, descriptions of some existing ones,
11 and how to write your own. Reusable actions are attributes on Catalyst
12 methods that allow you to decorate your method with functions running
13 before or after the method call. This can be used to implement
14 commonly used action patterns, while still leaving you full freedom to
15 customize them.
16
18 This is pretty simple. Actions work just like the normal dispatch
19 attributes you are used to, like Local or Private:
20
21 sub Hello :Local :ActionClass('SayBefore') {
22 $c->res->output( 'Hello '.$c->stash->{what} );
23 }
24
25 In this example, we expect the SayBefore action to magically populate
26 stash with something relevant before "Hello" is run. In the next
27 section we'll show you how to implement it. If you want it in a
28 namespace other than Catalyst::Action you can prefix the action name
29 with a '+', for instance '+Foo::SayBefore', or if you just want it
30 under your application namespace instead, use MyAction, like
31 MyAction('SayBefore').
32
34 Implementing the action itself is almost as easy. Just use
35 Catalyst::Action as a base class and decorate the "execute" call in the
36 Action class:
37
38 package Catalyst::Action::MyAction;
39 use Moose;
40 use namespace::autoclean;
41
42 extends 'Catalyst::Action';
43
44 before 'execute' => sub {
45 my ( $self, $controller, $c, $test ) = @_;
46 $c->stash->{what} = 'world';
47 };
48
49 after 'execute' => sub {
50 my ( $self, $controller, $c, $test ) = @_;
51 $c->stash->{foo} = 'bar';
52 };
53
54 __PACKAGE__->meta->make_immutable;
55
56 Pretty simple, huh?
57
59 You can only have one action class per action, which can be somewhat
60 inflexible.
61
62 The solution to this is to use Catalyst::Controller::ActionRole, which
63 would make the example above look like this:
64
65 package Catalyst::ActionRole::MyActionRole;
66 use Moose::Role;
67
68 before 'execute' => sub {
69 my ( $self, $controller, $c, $test ) = @_;
70 $c->stash->{what} = 'world';
71 };
72
73 after 'execute' => sub {
74 my ( $self, $controller, $c, $test ) = @_;
75 $c->stash->{foo} = 'bar';
76 };
77
78 1;
79
80 and this would be used in a controller like this:
81
82 package MyApp::Controller::Foo;
83 use Moose;
84 use namespace::autoclean;
85 BEGIN { extends 'Catalyst::Controller::ActionRole'; }
86
87 sub foo : Does('MyActionRole') {
88 my ($self, $c) = @_;
89 }
90
91 1;
92
94 Catalyst::Action::RenderView
95 This is meant to decorate end actions. It's similar in operation to
96 Catalyst::Plugin::DefaultEnd, but allows you to decide on an action
97 level rather than on an application level where it should be run.
98
99 Catalyst::Action::REST
100 Provides additional syntax for dispatching based upon the HTTP method
101 of the request.
102
104 Catalyst::ActionRole::ACL
105 Provides ACLs for role membership by decorating your actions.
106
108 Catalyst Contributors, see Catalyst.pm
109
111 This library is free software. You can redistribute it and/or modify it
112 under the same terms as Perl itself.
113
114
115
116perl v5.32.0 2020-07-28 Catalyst::Manual::Actions(3)