a6fbf0f1a750653f0ad113148fe568413cbae061
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Response.pm
1 package Catalyst::Response;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5
6 __PACKAGE__->mk_accessors(qw/cookies body headers location status/);
7
8 *output = \&body;
9
10 sub content_encoding { shift->headers->content_encoding(@_) }
11 sub content_length   { shift->headers->content_length(@_) }
12 sub content_type     { shift->headers->content_type(@_) }
13 sub header           { shift->headers->header(@_) }
14
15 =head1 NAME
16
17 Catalyst::Response - Catalyst Response Class
18
19 =head1 SYNOPSIS
20
21     $res = $c->response;
22     $res->body;
23     $res->content_encoding;
24     $res->content_length;
25     $res->content_type;
26     $res->cookies;
27     $res->header;
28     $res->headers;
29     $res->output;
30     $res->redirect;
31     $res->status;
32     $res->write;
33
34 See also L<Catalyst::Application>.
35
36 =head1 DESCRIPTION
37
38 This is the Catalyst Response class, which provides a set of accessors
39 to response data.
40
41 =head1 METHODS
42
43 =over 4
44
45 =item $res->body($text)
46
47     $c->response->body('Catalyst rocks!');
48
49 Contains the final output.
50
51 =item $res->content_encoding
52
53 Shortcut to $res->headers->content_encoding
54
55 =item $res->content_length
56
57 Shortcut to $res->headers->content_length
58
59 =item $res->content_type
60
61 Shortcut to $res->headers->content_type
62
63 =item $res->cookies
64
65 Returns a reference to a hash containing the cookies to be set. The keys of the
66 hash are the cookies' names, and their corresponding values are hash references
67 used to construct L<CGI::Cookie> object.
68
69     $c->response->cookies->{foo} = { value => '123' };
70
71 The values correspond to the L<CGI::Cookie> parameters of the same name, except
72 they are used without a leading dash.
73
74 The proxied parameters are
75
76 =over 4
77
78 =item value
79
80 =item expires
81
82 =item domain
83
84 =item path
85
86 =item secure
87
88 =item 
89
90 =back
91
92 =item $res->header
93
94 Shortcut to $res->headers->header
95
96 =item $res->headers
97
98 Returns a L<HTTP::Headers> object containing the headers.
99
100     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
101
102 =item $res->output
103
104 Shortcut to $res->body
105
106 =item $res->redirect( $url, $status )
107
108 Contains a location to redirect to.
109
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;
117
118     if (@_) {
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 }
128
129 =item $res->status
130
131 Contains the HTTP status.
132
133     $c->response->status(404);
134     
135 =item $res->write( $data )
136
137 Writes $data to the output stream.
138
139 =cut
140
141 sub write { shift->{_context}->write(@_); }
142
143 =back
144
145 =head1 AUTHOR
146
147 Sebastian Riedel, C<sri@cpan.org>
148 Marcus Ramberg, C<mramberg@cpan.org>
149
150 =head1 COPYRIGHT
151
152 This program is free software, you can redistribute it and/or modify 
153 it under the same terms as Perl itself.
154
155 =cut
156
157 1;