Reformatted documentation
[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 =head2 $res->cookies
60
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.
64
65     $c->response->cookies->{foo} = { value => '123' };
66
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:
70
71 =head2 value
72
73 =head2 expires
74
75 =head2 domain
76
77 =head2 path
78
79 =head2 secure
80
81 =head2 $res->header
82
83 Shortcut for $res->headers->header.
84
85 =head2 $res->headers
86
87 Returns an L<HTTP::Headers> object, which can be used to set headers.
88
89     $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
90
91 =head2 $res->output
92
93 Alias for $res->body.
94
95 =head2 $res->redirect( $url, $status )
96
97 Causes the response to redirect to the specified URL.
98
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;
106
107     if (@_) {
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 }
117
118 =head2 $res->status
119
120 Sets or returns the HTTP status.
121
122     $c->response->status(404);
123     
124 =head2 $res->write( $data )
125
126 Writes $data to the output stream.
127
128 =cut
129
130 sub write { shift->{_context}->write(@_); }
131
132 =head1 AUTHORS
133
134 Sebastian Riedel, C<sri@cpan.org>
135
136 Marcus Ramberg, C<mramberg@cpan.org>
137
138 =head1 COPYRIGHT
139
140 This program is free software, you can redistribute it and/or modify 
141 it under the same terms as Perl itself.
142
143 =cut
144
145 1;