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 |
37 | the current client request. | |
b22c6668 | 38 | |
39 | =head1 METHODS | |
fc7ec1d9 | 40 | |
b5ecfcf0 | 41 | =head2 $res->body($text) |
e060fe05 | 42 | |
43 | $c->response->body('Catalyst rocks!'); | |
06e1b616 | 44 | |
910410b8 | 45 | Sets or returns the output (text or binary data). |
06e1b616 | 46 | |
b5ecfcf0 | 47 | =head2 $res->content_encoding |
b5176d9e | 48 | |
910410b8 | 49 | Shortcut for $res->headers->content_encoding. |
b5176d9e | 50 | |
b5ecfcf0 | 51 | =head2 $res->content_length |
b5176d9e | 52 | |
910410b8 | 53 | Shortcut for $res->headers->content_length. |
b5176d9e | 54 | |
b5ecfcf0 | 55 | =head2 $res->content_type |
b5176d9e | 56 | |
910410b8 | 57 | Shortcut for $res->headers->content_type. |
b5176d9e | 58 | |
b5ecfcf0 | 59 | =head2 $res->cookies |
fc7ec1d9 | 60 | |
910410b8 | 61 | Returns a reference to a hash containing cookies to be set. The keys of the |
62 | hash are the cookies' names, and their corresponding values are hash | |
63 | references used to construct a L<CGI::Cookie> object. | |
fc7ec1d9 | 64 | |
65 | $c->response->cookies->{foo} = { value => '123' }; | |
66 | ||
910410b8 | 67 | The keys of the hash reference on the right correspond to the L<CGI::Cookie> |
68 | parameters of the same name, except they are used without a leading dash. | |
69 | Possible parameters are: | |
ac965e92 | 70 | |
b5ecfcf0 | 71 | =head2 value |
ac965e92 | 72 | |
b5ecfcf0 | 73 | =head2 expires |
ac965e92 | 74 | |
b5ecfcf0 | 75 | =head2 domain |
ac965e92 | 76 | |
b5ecfcf0 | 77 | =head2 path |
ac965e92 | 78 | |
b5ecfcf0 | 79 | =head2 secure |
ac965e92 | 80 | |
b5ecfcf0 | 81 | =head2 $res->header |
fbcc39ad | 82 | |
910410b8 | 83 | Shortcut for $res->headers->header. |
fbcc39ad | 84 | |
b5ecfcf0 | 85 | =head2 $res->headers |
fc7ec1d9 | 86 | |
910410b8 | 87 | Returns an L<HTTP::Headers> object, which can be used to set headers. |
fc7ec1d9 | 88 | |
89 | $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); | |
90 | ||
b5ecfcf0 | 91 | =head2 $res->output |
fc7ec1d9 | 92 | |
910410b8 | 93 | Alias for $res->body. |
fc7ec1d9 | 94 | |
b5ecfcf0 | 95 | =head2 $res->redirect( $url, $status ) |
fc7ec1d9 | 96 | |
910410b8 | 97 | Causes the response to redirect to the specified URL. |
fc7ec1d9 | 98 | |
73a52566 | 99 | $c->response->redirect( 'http://slashdot.org' ); |
100 | $c->response->redirect( 'http://slashdot.org', 307 ); | |
101 | ||
102 | =cut | |
103 | ||
104 | sub redirect { | |
105 | my $self = shift; | |
fbcc39ad | 106 | |
107 | if (@_) { | |
73a52566 | 108 | my $location = shift; |
109 | my $status = shift || 302; | |
110 | ||
111 | $self->location($location); | |
112 | $self->status($status); | |
113 | } | |
114 | ||
115 | return $self->location; | |
116 | } | |
fc7ec1d9 | 117 | |
b5ecfcf0 | 118 | =head2 $res->status |
fc7ec1d9 | 119 | |
910410b8 | 120 | Sets or returns the HTTP status. |
fc7ec1d9 | 121 | |
122 | $c->response->status(404); | |
fbcc39ad | 123 | |
b5ecfcf0 | 124 | =head2 $res->write( $data ) |
fbcc39ad | 125 | |
126 | Writes $data to the output stream. | |
127 | ||
128 | =cut | |
129 | ||
130 | sub write { shift->{_context}->write(@_); } | |
fc7ec1d9 | 131 | |
910410b8 | 132 | =head1 AUTHORS |
fc7ec1d9 | 133 | |
134 | Sebastian Riedel, C<sri@cpan.org> | |
910410b8 | 135 | |
61b1e958 | 136 | Marcus Ramberg, C<mramberg@cpan.org> |
fc7ec1d9 | 137 | |
138 | =head1 COPYRIGHT | |
139 | ||
61b1e958 | 140 | This program is free software, you can redistribute it and/or modify |
141 | it under the same terms as Perl itself. | |
fc7ec1d9 | 142 | |
143 | =cut | |
144 | ||
145 | 1; |