2 Catalyst::Component::ACCEPT_CONTEXT - Make the current Catalyst request
3 context available in Models and Views.
9 Models and Views don't usually have access to the request object, since
10 they probably don't really need it. Sometimes, however, having the
11 request context available outside of Controllers makes your application
12 cleaner. If that's the case, just use this module as a base class:
14 package MyApp::Model::Foobar;
15 use base qw|Catalyst::Component::ACCEPT_CONTEXT Catalyst::Model|;
17 Then, you'll be able to get the current request object from within your
22 print "The current URL is ". $self->context->req->uri->as_string;
25 WARNING WARNING WARNING
26 Using this module is somewhat of a hack. Changing the state of your
27 objects on every request is a pretty braindead way of doing OO. If you
28 want your application to be brain-live, then you should use
29 Catalyst::Component::InstancePerContext.
31 Instead of doing this on every request (which is basically what this
34 $my_component->context($c);
36 It's better to do something like this:
38 package FooApp::Controller::Root;
39 use base 'Catalyst::Controller';
42 with 'Catalyst::Component::InstancePerContext';
43 has 'context' => (is => 'ro');
45 sub build_per_context_instance {
46 my ($self, $c, @args) = @_;
47 return $self->new({ context => $c, %$self, @args });
50 sub actions :Whatever {
52 my $c = $self->context; # this works now
57 Now you get a brand new object that lasts for a single request instead
58 of changing the state of an existing one on each request. This is much
61 The best strategy, though, is not to use the context inside your model.
62 It's best for your Controller to pull the necessary data from the
63 context, and pass it as arguments:
67 my $foo = $c->model('Foo');
68 my $quux = $foo->frobnicate(baz => $c->request->params->{baz});
69 $c->stash->{quux} = $quux;
72 This will make it Really Easy to test your components outside of
73 Catalyst, which is always good.
77 Returns the current request context.
80 Catalyst calls this method to give the current context to your model.
81 You should never call it directly.
83 Note that a new instance of your component isn't created. All we do here
84 is shove $c into your component. ACCEPT_CONTEXT allows for other
85 behavior that may be more useful; if you want something else to happen
86 just implement it yourself.
88 See Catalyst::Component for details.
91 Overridden to use initial application object as context before a
95 Jonathan Rockway, "<jrockway at cpan.org>"
97 Patches contributed and maintained by:
99 Rafael Kitover (Caelum)
100 Tomas Doran (t0m) "<bobtfish@bobtfish.net>"
103 Please report any bugs or feature requests to
104 "bug-catalyst-component-accept_context at rt.cpan.org", or through the
106 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Catalyst-Component-ACCEP
107 T_CONTEXT>. I will be notified, and then you'll automatically be
108 notified of progress on your bug as I make changes.
111 You can find documentation for this module with the perldoc command.
113 perldoc Catalyst::Component::ACCEPT_CONTEXT
115 You can also look for information at:
118 <http://www.catalystframework.org/>
120 * AnnoCPAN: Annotated CPAN documentation
121 <http://annocpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
124 <http://cpanratings.perl.org/d/Catalyst-Component-ACCEPT_CONTEXT>
126 * RT: CPAN's request tracker
127 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Component-ACCEPT_
131 <http://search.cpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
134 The source code for this project can be found at:
136 git://git.shadowcat.co.uk/catagits/Catalyst-Component-ACCEPT_CONTEXT
139 Copyright 2007 Jonathan Rockway.
141 This program is free software; you can redistribute it and/or modify it
142 under the same terms as Perl itself.