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 | ||
17 | Catalyst::Response - Catalyst Response Class | |
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; | |
27 | $res->handle; | |
28 | $res->header; | |
29 | $res->headers; | |
30 | $res->output; | |
31 | $res->redirect; | |
32 | $res->status; | |
33 | $res->write; | |
b22c6668 | 34 | |
35 | See also L<Catalyst::Application>. | |
fc7ec1d9 | 36 | |
37 | =head1 DESCRIPTION | |
38 | ||
61b1e958 | 39 | This is the Catalyst Response class, which provides a set of accessors |
40 | to response data. | |
b22c6668 | 41 | |
42 | =head1 METHODS | |
fc7ec1d9 | 43 | |
b22c6668 | 44 | =over 4 |
fc7ec1d9 | 45 | |
fbcc39ad | 46 | =item $res->body($text) |
e060fe05 | 47 | |
48 | $c->response->body('Catalyst rocks!'); | |
06e1b616 | 49 | |
e060fe05 | 50 | Contains the final output. |
06e1b616 | 51 | |
fbcc39ad | 52 | =item $res->content_encoding |
b5176d9e | 53 | |
fbcc39ad | 54 | Shortcut to $res->headers->content_encoding |
b5176d9e | 55 | |
fbcc39ad | 56 | =item $res->content_length |
b5176d9e | 57 | |
fbcc39ad | 58 | Shortcut to $res->headers->content_length |
b5176d9e | 59 | |
fbcc39ad | 60 | =item $res->content_type |
b5176d9e | 61 | |
fbcc39ad | 62 | Shortcut to $res->headers->content_type |
b5176d9e | 63 | |
fbcc39ad | 64 | =item $res->cookies |
fc7ec1d9 | 65 | |
61b1e958 | 66 | Returns a reference to a hash containing the cookies to be set. |
fc7ec1d9 | 67 | |
68 | $c->response->cookies->{foo} = { value => '123' }; | |
69 | ||
fbcc39ad | 70 | =item $res->handle |
b5176d9e | 71 | |
fbcc39ad | 72 | Response IO handle. |
b5176d9e | 73 | |
fbcc39ad | 74 | =cut |
75 | ||
76 | sub handle { | |
77 | my ( $self, $handle ) = @_; | |
78 | ||
79 | if ($handle) { | |
80 | $self->{handle} = $handle; | |
81 | } | |
82 | else { | |
83 | # Finalize headers if someone touches the output handle | |
84 | if ( $self->{_context} ) { | |
85 | $self->{_context}->finalize_headers; | |
86 | } | |
87 | } | |
88 | ||
89 | return $self->{handle}; | |
90 | } | |
91 | ||
92 | =item $res->header | |
93 | ||
94 | Shortcut to $res->headers->header | |
95 | ||
96 | =item $res->headers | |
fc7ec1d9 | 97 | |
98 | Returns a L<HTTP::Headers> object containing the headers. | |
99 | ||
100 | $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION ); | |
101 | ||
fbcc39ad | 102 | =item $res->output |
fc7ec1d9 | 103 | |
fbcc39ad | 104 | Shortcut to $res->body |
fc7ec1d9 | 105 | |
fbcc39ad | 106 | =item $res->redirect( $url, $status ) |
fc7ec1d9 | 107 | |
108 | Contains a location to redirect to. | |
109 | ||
73a52566 | 110 | $c->response->redirect( 'http://slashdot.org' ); |
111 | $c->response->redirect( 'http://slashdot.org', 307 ); | |
112 | ||
113 | =cut | |
114 | ||
115 | sub redirect { | |
116 | my $self = shift; | |
fbcc39ad | 117 | |
118 | if (@_) { | |
73a52566 | 119 | my $location = shift; |
120 | my $status = shift || 302; | |
121 | ||
122 | $self->location($location); | |
123 | $self->status($status); | |
124 | } | |
125 | ||
126 | return $self->location; | |
127 | } | |
fc7ec1d9 | 128 | |
fbcc39ad | 129 | =item $res->status |
fc7ec1d9 | 130 | |
131 | Contains the HTTP status. | |
132 | ||
133 | $c->response->status(404); | |
fbcc39ad | 134 | |
135 | =item $res->write( $data ) | |
136 | ||
137 | Writes $data to the output stream. | |
138 | ||
139 | =cut | |
140 | ||
141 | sub write { shift->{_context}->write(@_); } | |
fc7ec1d9 | 142 | |
b22c6668 | 143 | =back |
144 | ||
fc7ec1d9 | 145 | =head1 AUTHOR |
146 | ||
147 | Sebastian Riedel, C<sri@cpan.org> | |
61b1e958 | 148 | Marcus Ramberg, C<mramberg@cpan.org> |
fc7ec1d9 | 149 | |
150 | =head1 COPYRIGHT | |
151 | ||
61b1e958 | 152 | This program is free software, you can redistribute it and/or modify |
153 | it under the same terms as Perl itself. | |
fc7ec1d9 | 154 | |
155 | =cut | |
156 | ||
157 | 1; |