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 setup_actions(),
103 but this should be done with due consideration and as late as possible.
104
106 To activate Catalyst's action attributes, Moose-ified controller
107 classes need to extend Catalyst::Controller at compile time, before the
108 actions themselves are declared:
109
110 package Catalyst::Controller::Root;
111 use Moose;
112 use namespace::autoclean;
113
114 BEGIN { extends 'Catalyst::Controller'; }
115
116 Controller Roles
117 It is possible to use roles to apply method modifiers on controller
118 actions from 5.80003 onwards, or use modifiers in your controller
119 classes themselves. For example
120
121 package MyApp::Controller::Foo;
122 use Moose;
123 use namespace::autoclean;
124 BEGIN { extends 'Catalyst::Controller' };
125
126 sub foo : Local {
127 my ($self, $c) = @_;
128 $c->res->body('Hello ');
129 }
130 after foo => sub {
131 my ($self, $c) = @_;
132 $c->res->body($c->res->body . 'World');
133 };
134
135 It is possible to have action methods with attributes inside Moose
136 roles, using MooseX::MethodAttributes, example:
137
138 package MyApp::ControllerRole;
139 use MooseX::MethodAttributes::Role;
140 use namespace::autoclean;
141
142 sub foo : Local {
143 my ($self, $c) = @_;
144 ...
145 }
146
147 package MyApp::Controller::Foo;
148 use Moose;
149 use namespace::autoclean;
150 BEGIN { extends 'Catalyst::Controller' };
151
152 with 'MyApp::ControllerRole';
153
155 Catalyst Contributors, see Catalyst.pm
156
158 This library is free software. You can redistribute it and/or modify it
159 under the same terms as Perl itself.
160
161
162
163perl v5.36.0 2023-01-C2a0talyst::Manual::CatalystAndMoose(3)