Tweaks
[catagits/Catalyst-Component-ACCEPT_CONTEXT.git] / README
1 NAME
2     Catalyst::Component::ACCEPT_CONTEXT - Make the current Catalyst request
3     context available in Models and Views.
4
5 VERSION
6     Version 0.07
7
8 SYNOPSIS
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:
13
14         package MyApp::Model::Foobar;
15         use base qw|Catalyst::Component::ACCEPT_CONTEXT Catalyst::Model|;
16
17     Then, you'll be able to get the current request object from within your
18     model:
19
20         sub do_something {
21             my $self = shift;
22             print "The current URL is ". $self->context->req->uri->as_string;
23         }
24
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.
30
31     Instead of doing this on every request (which is basically what this
32     module does):
33
34         $my_component->context($c);
35
36     It's better to do something like this:
37
38         package FooApp::Controller::Root;
39         use base 'Catalyst::Controller';
40         use Moose;
41
42         with 'Catalyst::Component::InstancePerContext';
43         has 'context' => (is => 'ro');
44
45         sub build_per_context_instance {
46             my ($self, $c, @args) = @_;
47             return $self->new({ context => $c, %$self, @args });
48         }
49
50         sub actions :Whatever {
51             my $self = shift;
52             my $c = $self->context; # this works now
53         }
54
55         1;
56
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
59     cleaner OO design.
60
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:
64
65        sub action :Local {
66            my ($self, $c) = @_;
67            my $foo  = $c->model('Foo');
68            my $quux = $foo->frobnicate(baz => $c->request->params->{baz});
69            $c->stash->{quux} = $quux;
70        }
71
72     This will make it Really Easy to test your components outside of
73     Catalyst, which is always good.
74
75 METHODS
76   context
77     Returns the current request context.
78
79   ACCEPT_CONTEXT
80     Catalyst calls this method to give the current context to your model.
81     You should never call it directly.
82
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.
87
88     See Catalyst::Component for details.
89
90   COMPONENT
91     Overridden to use initial application object as context before a
92     request.
93
94 AUTHOR
95     Jonathan Rockway, "<jrockway at cpan.org>"
96
97     Patches contributed and maintained by:
98
99     Rafael Kitover (Caelum)
100     Tomas Doran (t0m) "<bobtfish@bobtfish.net>"
101
102 BUGS
103     Please report any bugs or feature requests to
104     "bug-catalyst-component-accept_context at rt.cpan.org", or through the
105     web interface at
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.
109
110 SUPPORT
111     You can find documentation for this module with the perldoc command.
112
113         perldoc Catalyst::Component::ACCEPT_CONTEXT
114
115     You can also look for information at:
116
117     * Catalyst Website
118         <http://www.catalystframework.org/>
119
120     * AnnoCPAN: Annotated CPAN documentation
121         <http://annocpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
122
123     * CPAN Ratings
124         <http://cpanratings.perl.org/d/Catalyst-Component-ACCEPT_CONTEXT>
125
126     * RT: CPAN's request tracker
127         <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Catalyst-Component-ACCEPT_
128         CONTEXT>
129
130     * Search CPAN
131         <http://search.cpan.org/dist/Catalyst-Component-ACCEPT_CONTEXT>
132
133 Source code
134     The source code for this project can be found at:
135
136         git://git.shadowcat.co.uk/catagits/Catalyst-Component-ACCEPT_CONTEXT
137
138 COPYRIGHT & LICENSE
139     Copyright 2007 Jonathan Rockway.
140
141     This program is free software; you can redistribute it and/or modify it
142     under the same terms as Perl itself.
143