1 package Catalyst::Component::ACCEPT_CONTEXT;
6 use Scalar::Util qw(weaken);
10 Catalyst::Component::ACCEPT_CONTEXT - Make the current Catalyst
11 request context available in Models and Views.
19 our $VERSION = '0.05';
23 Models and Views don't usually have access to the request object,
24 since they probably don't really need it. Sometimes, however, having
25 the request context available outside of Controllers makes your
26 application cleaner. If that's the case, just use this module as a
29 package MyApp::Model::Foobar;
30 use base qw|Catalyst::Component::ACCEPT_CONTEXT Catalyst::Model|;
32 Then, you'll be able to get the current request object from within
37 print "The current URL is ". $self->context->req->uri->as_string;
40 =head1 WARNING WARNING WARNING
42 Using this module is somewhat of a hack. Changing the state of your
43 objects on every request is a pretty braindead way of doing OO. If
44 you want your application to be brain-live, then you should use
45 L<Catalyst::Component::InstancePerContext|Catalyst::Component::InstancePerContext>.
47 Instead of doing this on every request (which is basically
48 what this module does):
50 $my_component->context($c);
52 It's better to do something like this:
54 package FooApp::Controller::Root;
55 use base 'Catalyst::Controller';
58 with 'Catalyst::Component::InstancePerContext';
59 has 'context' => (is => 'ro');
61 sub build_per_context_instance {
62 my ($self, $c, @args) = @_;
63 return $self->new({ context => $c, %$self, @args });
66 sub actions :Whatever {
68 my $c = $self->context; # this works now
73 Now you get a brand new object that lasts for a single request instead
74 of changing the state of an existing one on each request. This is
75 much cleaner OO design.
77 The best strategy, though, is not to use the context inside your
78 model. It's best for your Controller to pull the necessary data from
79 the context, and pass it as arguments:
83 my $foo = $c->model('Foo');
84 my $quux = $foo->frobnicate(baz => $c->request->params->{baz});
85 $c->stash->{quux} = $quux;
88 This will make it Really Easy to test your components outside of
89 Catalyst, which is always good.
95 Returns the current request context.
100 return shift->{context};
103 =head2 ACCEPT_CONTEXT
105 Catalyst calls this method to give the current context to your model.
106 You should never call it directly.
108 Note that a new instance of your component isn't created. All we do
109 here is shove C<$c> into your component. ACCEPT_CONTEXT allows for
110 other behavior that may be more useful; if you want something else to
111 happen just implement it yourself.
113 See L<Catalyst::Component> for details.
121 $self->{context} = $context;
122 weaken($self->{context});
124 return $self->NEXT::ACCEPT_CONTEXT($context, @_) || $self;
129 Overridden to use initial application object as context before a request.
137 $args->{context} = $app;
138 weaken($args->{context}) if ref $args->{context};
139 return $class->NEXT::COMPONENT($app, $args, @_);
144 Jonathan Rockway, C<< <jrockway at cpan.org> >>
148 Please report any bugs or feature requests to
149 C<bug-catalyst-component-accept_context at rt.cpan.org>, or through the web interface at
150 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Component-ACCEPT_CONTEXT>.
151 I will be notified, and then you'll automatically be notified of progress on
152 your bug as I make changes.
156 You can find documentation for this module with the perldoc command.
158 perldoc Catalyst::Component::ACCEPT_CONTEXT
160 You can also look for information at:
164 =item * Catalyst Website
166 L<http://www.catalystframework.org/>
168 =item * AnnoCPAN: Annotated CPAN documentation
170 L<http://annocpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
174 L<http://cpanratings.perl.org/d/Catalyst-Component-ACCEPT_CONTEXT>
176 =item * RT: CPAN's request tracker
178 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Component-ACCEPT_CONTEXT>
182 L<http://search.cpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
186 =head1 COPYRIGHT & LICENSE
188 Copyright 2007 Jonathan Rockway.
190 This program is free software; you can redistribute it and/or modify it
191 under the same terms as Perl itself.
195 1; # End of Catalyst::Component::ACCEPT_CONTEXT