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