Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Response; |
2 | |
3 | use strict; |
4 | use base 'Class::Accessor::Fast'; |
5 | |
73a52566 |
6 | __PACKAGE__->mk_accessors(qw/cookies body headers location status/); |
e060fe05 |
7 | |
8 | *output = \&body; |
fc7ec1d9 |
9 | |
f7e4e231 |
10 | sub content_encoding { shift->headers->content_encoding(@_) } |
fbcc39ad |
11 | sub content_length { shift->headers->content_length(@_) } |
12 | sub content_type { shift->headers->content_type(@_) } |
13 | sub header { shift->headers->header(@_) } |
f7e4e231 |
14 | |
fc7ec1d9 |
15 | =head1 NAME |
16 | |
910410b8 |
17 | Catalyst::Response - stores output responding to the current client request |
fc7ec1d9 |
18 | |
19 | =head1 SYNOPSIS |
20 | |
fbcc39ad |
21 | $res = $c->response; |
22 | $res->body; |
23 | $res->content_encoding; |
24 | $res->content_length; |
25 | $res->content_type; |
26 | $res->cookies; |
fbcc39ad |
27 | $res->header; |
28 | $res->headers; |
29 | $res->output; |
30 | $res->redirect; |
31 | $res->status; |
32 | $res->write; |
b22c6668 |
33 | |
fc7ec1d9 |
34 | =head1 DESCRIPTION |
35 | |
910410b8 |
36 | This is the Catalyst Response class, which provides methods for responding to |
46372e65 |
37 | the current client request. The appropriate L<Catalyst::Engine> for your environment |
38 | will turn the Catalyst::Response into a HTTP Response and return it to the client. |
b22c6668 |
39 | |
40 | =head1 METHODS |
fc7ec1d9 |
41 | |
5f7b140f |
42 | =head2 $res->body(<$text|$fh|$iohandle_object) |
e060fe05 |
43 | |
44 | $c->response->body('Catalyst rocks!'); |
06e1b616 |
45 | |
46372e65 |
46 | Sets or returns the output (text or binary data). If you are returning a large body, |
5f7b140f |
47 | you might want to use a L<IO::Handle> type of object (Something that implements the read method |
46372e65 |
48 | in the same fashion), or a filehandle GLOB. Catalyst |
49 | will write it piece by piece into the response. |
06e1b616 |
50 | |
b5ecfcf0 |
51 | =head2 $res->content_encoding |
b5176d9e |
52 | |
910410b8 |
53 | Shortcut for $res->headers->content_encoding. |
b5176d9e |
54 | |
b5ecfcf0 |
55 | =head2 $res->content_length |
b5176d9e |
56 | |
910410b8 |
57 | Shortcut for $res->headers->content_length. |
b5176d9e |
58 | |
b5ecfcf0 |
59 | =head2 $res->content_type |
b5176d9e |
60 | |
910410b8 |
61 | Shortcut for $res->headers->content_type. |
b5176d9e |
62 | |
87e9f9ab |
63 | This value is typically set by your view or plugin. For example, |
64 | L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file |
65 | it found, while L<Catalyst::View::TT> defaults to C<text/html>. |
66 | |
b5ecfcf0 |
67 | =head2 $res->cookies |
fc7ec1d9 |
68 | |
910410b8 |
69 | Returns a reference to a hash containing cookies to be set. The keys of the |
70 | hash are the cookies' names, and their corresponding values are hash |
71 | references used to construct a L<CGI::Cookie> object. |
fc7ec1d9 |
72 | |
73 | $c->response->cookies->{foo} = { value => '123' }; |
74 | |
910410b8 |
75 | The keys of the hash reference on the right correspond to the L<CGI::Cookie> |
76 | parameters of the same name, except they are used without a leading dash. |
77 | Possible parameters are: |
ac965e92 |
78 | |
71453caf |
79 | =over |
ac965e92 |
80 | |
71453caf |
81 | =item value |
ac965e92 |
82 | |
71453caf |
83 | =item expires |
ac965e92 |
84 | |
71453caf |
85 | =item domain |
ac965e92 |
86 | |
71453caf |
87 | =item path |
88 | |
89 | =item secure |
90 | |
91 | =back |
ac965e92 |
92 | |
b5ecfcf0 |
93 | =head2 $res->header |
fbcc39ad |
94 | |
910410b8 |
95 | Shortcut for $res->headers->header. |
fbcc39ad |
96 | |
b5ecfcf0 |
97 | =head2 $res->headers |
fc7ec1d9 |
98 | |
910410b8 |
99 | Returns an L<HTTP::Headers> object, which can be used to set headers. |
fc7ec1d9 |
100 | |
101 | $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); |
102 | |
b5ecfcf0 |
103 | =head2 $res->output |
fc7ec1d9 |
104 | |
910410b8 |
105 | Alias for $res->body. |
fc7ec1d9 |
106 | |
b5ecfcf0 |
107 | =head2 $res->redirect( $url, $status ) |
fc7ec1d9 |
108 | |
dd8a0c31 |
109 | Causes the response to redirect to the specified URL. The default status is |
110 | C<302>. |
fc7ec1d9 |
111 | |
73a52566 |
112 | $c->response->redirect( 'http://slashdot.org' ); |
113 | $c->response->redirect( 'http://slashdot.org', 307 ); |
114 | |
2d07aec5 |
115 | This is a convenience method that sets the Location header to the |
116 | redirect destination, and then sets the response status. You will |
117 | want to C< return; > or C< $c->detach() > to interrupt the normal |
118 | processing flow if you want the redirect to occur straight away. |
119 | |
73a52566 |
120 | =cut |
121 | |
122 | sub redirect { |
123 | my $self = shift; |
fbcc39ad |
124 | |
125 | if (@_) { |
73a52566 |
126 | my $location = shift; |
f1bbebac |
127 | my $status = shift || 302; |
73a52566 |
128 | |
129 | $self->location($location); |
130 | $self->status($status); |
131 | } |
132 | |
133 | return $self->location; |
134 | } |
fc7ec1d9 |
135 | |
b5ecfcf0 |
136 | =head2 $res->status |
fc7ec1d9 |
137 | |
910410b8 |
138 | Sets or returns the HTTP status. |
fc7ec1d9 |
139 | |
140 | $c->response->status(404); |
fbcc39ad |
141 | |
b5ecfcf0 |
142 | =head2 $res->write( $data ) |
fbcc39ad |
143 | |
144 | Writes $data to the output stream. |
145 | |
146 | =cut |
147 | |
148 | sub write { shift->{_context}->write(@_); } |
fc7ec1d9 |
149 | |
910410b8 |
150 | =head1 AUTHORS |
fc7ec1d9 |
151 | |
0bf7ab71 |
152 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
153 | |
154 | =head1 COPYRIGHT |
155 | |
61b1e958 |
156 | This program is free software, you can redistribute it and/or modify |
157 | it under the same terms as Perl itself. |
fc7ec1d9 |
158 | |
159 | =cut |
160 | |
161 | 1; |