1 package Catalyst::Response;
6 with 'MooseX::Emulate::Class::Accessor::Fast';
8 has cookies => (is => 'rw', default => sub { {} });
9 has body => (is => 'rw', default => undef);
10 sub has_body { defined($_[0]->body) }
12 has location => (is => 'rw');
13 has status => (is => 'rw', default => 200);
14 has finalized_headers => (is => 'rw', default => 0);
17 handles => [qw(content_encoding content_length content_type header)],
18 default => sub { HTTP::Headers->new() },
26 clearer => '_clear_context',
29 sub output { shift->body(@_) }
31 sub code { shift->status(@_) }
37 Catalyst::Response - stores output responding to the current client request
44 $res->content_encoding;
57 This is the Catalyst Response class, which provides methods for responding to
58 the current client request. The appropriate L<Catalyst::Engine> for your environment
59 will turn the Catalyst::Response into a HTTP Response and return it to the client.
63 =head2 $res->body( $text | $fh | $iohandle_object )
65 $c->response->body('Catalyst rocks!');
67 Sets or returns the output (text or binary data). If you are returning a large body,
68 you might want to use a L<IO::Handle> type of object (Something that implements the read method
69 in the same fashion), or a filehandle GLOB. Catalyst
70 will write it piece by piece into the response.
74 Predicate which returns true when a body has been set.
78 Alias for $res->status.
80 =head2 $res->content_encoding
82 Shortcut for $res->headers->content_encoding.
84 =head2 $res->content_length
86 Shortcut for $res->headers->content_length.
88 =head2 $res->content_type
90 Shortcut for $res->headers->content_type.
92 This value is typically set by your view or plugin. For example,
93 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
94 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
98 Returns a reference to a hash containing cookies to be set. The keys of the
99 hash are the cookies' names, and their corresponding values are hash
100 references used to construct a L<CGI::Simple::Cookie> object.
102 $c->response->cookies->{foo} = { value => '123' };
104 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
105 parameters of the same name, except they are used without a leading dash.
106 Possible parameters are:
126 Shortcut for $res->headers->header.
130 Returns an L<HTTP::Headers> object, which can be used to set headers.
132 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
136 Alias for $res->body.
138 =head2 $res->redirect( $url, $status )
140 Causes the response to redirect to the specified URL. The default status is
143 $c->response->redirect( 'http://slashdot.org' );
144 $c->response->redirect( 'http://slashdot.org', 307 );
146 This is a convenience method that sets the Location header to the
147 redirect destination, and then sets the response status. You will
148 want to C< return > or C<< $c->detach() >> to interrupt the normal
149 processing flow if you want the redirect to occur straight away.
157 my $location = shift;
158 my $status = shift || 302;
160 $self->location($location);
161 $self->status($status);
164 return $self->location;
167 =head2 $res->location
169 Sets or returns the HTTP 'Location'.
173 Sets or returns the HTTP status.
175 $c->response->status(404);
177 $res->code is an alias for this, to match HTTP::Response->code.
179 =head2 $res->write( $data )
181 Writes $data to the output stream.
187 =head2 $res->print( @data )
189 Prints @data to the output stream, separated by $,. This lets you pass
190 the response object to functions that want to write to an L<IO::Handle>.
198 defined $self->write($data) or return;
201 defined $self->write($,) or return;
202 defined $self->write($_) or return;
204 defined $self->write($\) or return;
211 Catalyst Contributors, see Catalyst.pm
215 This library is free software. You can redistribute it and/or modify
216 it under the same terms as Perl itself.
220 __PACKAGE__->meta->make_immutable;