3 Catalyst::Manual::CatalystAndMoose - How Catalyst 5.8+ and Moose relate
8 Since version 5.8 the core of Catalyst is based on L<Moose>. Although
9 the developers went through great lengths to allow for a seamless
10 transition, there are still a few things to keep in mind when trying
11 to exploit the power of L<Moose> in your Catalyst application.
13 This document provides you with a short overview of common caveats and
14 best practices to use L<Moose>-based classes within Catalyst.
17 =head1 THE CONTEXT CLASS
19 A Moose-ified version of the context class should look like this:
24 use namespace::autoclean;
26 # your roles and plugins
29 $app->config( name => 'MyApp' );
32 # method modifiers must be created after setup because otherwise they will
33 # conflict with plugin overrides
35 after 'finalize' => sub{
37 $c->log->info( 'done!' );
40 You should also be aware, that roles in C<< $c-E<gt>setup >> are applied
41 after the last plugin with all the benefits of using a single
42 C<< with() >> statement in an ordinary L<Moose> class.
44 Your class is automatically made immutable at the end of the current file.
46 CAVEAT: Using roles in C<< $c-E<gt>setup >> was implemented in Catalyst
47 version 5.80004. In prior versions you might get away with
49 after 'setup_plugins' => sub{ with(
57 but this is discouraged and you should upgrade to 5.80004 anyway,
58 because it fixes a few important regression against 5.71
60 CAVEAT: Using roles in C<< $c-E<gt>setup >> will not currently allow
61 you to pass parameters to roles, or perform conflict resolution.
62 Conflict detection still works as expected.
67 Most of the request-specific attributes like C<$c-E<gt>stash>,
68 C<$c-E<gt>request> and C<$c-E<gt>response> have been converted to
69 L<Moose> attributes but without type constraints, attribute helpers or
70 builder methods. This ensures that Catalyst 5.8 is fully backwards
71 compatible to applications using the published API of Catalyst 5.7 but
72 slightly limits the gains that could be had by wielding the full power
73 of L<Moose> attributes.
75 Most of the accessors to information gathered during compile time is
76 managed by C<Catalyst::ClassData>, which is a L<Moose>-aware version
77 of L<Class::Data::Inheritable> but not compatible with
78 L<MooseX::ClassAttribute>.
81 =head2 ROLES AND METHOD MODIFIERS
83 Since the release of Catalyst version 5.8 the only reason for creating
84 a Catalyst extension as a plugin is to provide backward compatibility
85 to applications still using version 5.7.
87 If backward compatibility is of no concern to you, you could as easily
88 rewrite your plugins as roles and enjoy all the benefits of automatic
89 method re-dispatching of C<< before >> and C<< after >> method
90 modifiers, naming conflict detecting and generally cleaner code.
92 Plugins and roles should never use
94 after 'setup' => sub { ... } # wrong
98 after 'setup_finalize' => sub { ... } # this will work
100 to run their own setup code if needed. If they need to influence the
101 setup process itself, they can modify C<< setup_dispatcher() >>,
102 C<< setup_engine() >>, C<< setup_stats() >>, C<< setup_components() >>
103 and C<< setup_actions() >>, but this should be done with due
104 consideration and as late as possible.
109 To activate Catalyst's action attributes, Moose-ified controller
110 classes need to extend L<Catalyst::Controller> at compile time before
111 the actions themselves are declared:
113 package Catalyst::Controller::Root;
115 use namespace::autoclean;
117 BEGIN { extends 'Catalyst::Controller'; }
119 # your controller roles
123 =head2 Controller Roles
125 It is possible to use roles to apply method modifiers on controller actions
126 from 5.80003 onwards, or use modifiers in actions in your controller classes
129 It is possible to have action methods with attributes inside Moose roles, using
130 the trait introduced in L<MooseX::MethodAttributes> version 0.12, example:
132 package MyApp::ControllerRole;
133 use Moose::Role -traits => 'MethodAttributes';
134 use namespace::autoclean;