1Catalyst::Manual::CatalUyssetrAnCdoMnotorsieb(u3t)ed PerClatDaolcyusmte:n:tMaatniuoanl::CatalystAndMoose(3)
2
3
4
6 Catalyst::Manual::CatalystAndMoose - How Catalyst 5.8+ and Moose relate
7
9 Since version 5.8, the core of Catalyst is based on Moose. Although the
10 developers went through great lengths to allow for a seamless
11 transition, there are still a few things to keep in mind when trying to
12 exploit the power of Moose in your Catalyst application.
13
14 This document provides you with a short overview of common caveats and
15 best practices for using Moose-based classes within Catalyst.
16
18 A Moose-ified version of the context class should look like this:
19
20 package MyApp;
21 use Moose;
22 use namespace::autoclean;
23 use Catalyst (
24 # your roles and plugins
25 );
26 extends 'Catalyst';
27
28 # If you want to use method modifiers to adjust the setup process, (e.g. setup_finalize)
29 # they must be here, before the call to setup (advanced users only)
30
31 $app->config( name => 'MyApp' );
32 $app->setup;
33
34 # method modifiers generally must be created after setup because otherwise they will
35 # conflict with plugin overrides
36
37 after 'finalize' => sub {
38 my $c = shift;
39 $c->log->info( 'done!' );
40 }
41
42 You should also be aware that roles in "$c->setup" are applied after
43 the last plugin with all the benefits of using a single with()
44 statement in an ordinary Moose class.
45
46 Your class is automatically made immutable at the end of the current
47 file.
48
49 CAVEAT: Using roles in "$c->setup" was implemented in Catalyst version
50 5.80004. In prior versions you might get away with
51
52 after 'setup_plugins' => sub{ with(
53 # your roles
54 )};
55
56 $app->setup(
57 # your plugins
58 );
59
60 but this is discouraged and you should upgrade to 5.80004 anyway,
61 because it fixes a few important regressions against 5.71
62
63 CAVEAT: Using roles in "$c->setup" will not currently allow you to pass
64 parameters to roles, or perform conflict resolution. Conflict
65 detection still works as expected.
66
67 ACCESSORS
68 Most of the request-specific attributes like "$c->stash", "$c->request"
69 and "$c->response" have been converted to Moose attributes but without
70 type constraints, attribute helpers or builder methods. This ensures
71 that Catalyst 5.8 is fully backwards compatible to applications using
72 the published API of Catalyst 5.7 but slightly limits the gains that
73 could be had by wielding the full power of Moose attributes.
74
75 Most of the accessors to information gathered during compile time (such
76 as configuration) are managed by Catalyst::ClassData, which is a
77 Moose-aware version of Class::Data::Inheritable but not compatible with
78 MooseX::ClassAttribute.
79
80 ROLES AND METHOD MODIFIERS
81 Since the release of Catalyst version 5.8, the only reason for creating
82 a Catalyst extension as a plugin is to provide backward compatibility
83 to applications still using version 5.7.
84
85 If backward compatibility is of no concern to you, you could as easily
86 rewrite your plugins as roles and enjoy all the benefits of automatic
87 method re-dispatching of "before" and "after" method modifiers, naming
88 conflict detection and generally cleaner code.
89
90 NOTE
91
92 Plugins and roles should never use
93
94 after 'setup' => sub { ... } # wrong
95
96 (or any other method of hooking the setup method) but rely on
97
98 after 'setup_finalize' => sub { ... } # this will work
99
100 to run their own setup code if needed. If they need to influence the
101 setup process itself, they can modify "setup_dispatcher()",
102 "setup_engine()", "setup_stats()", "setup_components()" and
103 "setup_actions()", but this should be done with due consideration and
104 as late as possible.
105
107 To activate Catalyst's action attributes, Moose-ified controller
108 classes need to extend Catalyst::Controller at compile time, before the
109 actions themselves are declared:
110
111 package Catalyst::Controller::Root;
112 use Moose;
113 use namespace::autoclean;
114
115 BEGIN { extends 'Catalyst::Controller'; }
116
117 Controller Roles
118 It is possible to use roles to apply method modifiers on controller
119 actions from 5.80003 onwards, or use modifiers in your controller
120 classes themselves. For example
121
122 package MyApp::Controller::Foo;
123 use Moose;
124 use namespace::autoclean;
125 BEGIN { extends 'Catalyst::Controller' };
126
127 sub foo : Local {
128 my ($self, $c) = @_;
129 $c->res->body('Hello ');
130 }
131 after foo => sub {
132 my ($self, $c) = @_;
133 $c->res->body($c->res->body . 'World');
134 };
135
136 It is possible to have action methods with attributes inside Moose
137 roles, using MooseX::MethodAttributes, example:
138
139 package MyApp::ControllerRole;
140 use MooseX::MethodAttributes::Role;
141 use namespace::autoclean;
142
143 sub foo : Local {
144 my ($self, $c) = @_;
145 ...
146 }
147
148 package MyApp::Controller::Foo;
149 use Moose;
150 use namespace::autoclean;
151 BEGIN { extends 'Catalyst::Controller' };
152
153 with 'MyApp::ControllerRole';
154
156 Catalyst Contributors, see Catalyst.pm
157
159 This library is free software. You can redistribute it and/or modify it
160 under the same terms as Perl itself.
161
162
163
164perl v5.30.1 2020-01-C2a9talyst::Manual::CatalystAndMoose(3)