1 package Catalyst::Response;
6 with 'MooseX::Emulate::Class::Accessor::Fast';
8 has cookies => (is => 'rw', default => sub { {} });
9 has body => (is => 'rw', default => '', lazy => 1, predicate => 'has_body',
10 clearer => '_clear_body'
12 after 'body' => sub { # If someone assigned undef, clear the body so we get ''
13 if (scalar(@_) == 2 && !defined($_[1])) {
17 has location => (is => 'rw');
18 has status => (is => 'rw', default => 200);
19 has finalized_headers => (is => 'rw', default => 0);
22 handles => [qw(content_encoding content_length content_type header)],
23 default => sub { HTTP::Headers->new() },
31 clearer => '_clear_context',
34 sub output { shift->body(@_) }
36 sub code { shift->status(@_) }
42 Catalyst::Response - stores output responding to the current client request
49 $res->content_encoding;
62 This is the Catalyst Response class, which provides methods for responding to
63 the current client request. The appropriate L<Catalyst::Engine> for your environment
64 will turn the Catalyst::Response into a HTTP Response and return it to the client.
68 =head2 $res->body(<$text|$fh|$iohandle_object)
70 $c->response->body('Catalyst rocks!');
72 Sets or returns the output (text or binary data). If you are returning a large body,
73 you might want to use a L<IO::Handle> type of object (Something that implements the read method
74 in the same fashion), or a filehandle GLOB. Catalyst
75 will write it piece by piece into the response.
79 Predicate which returns true when a body has been set.
83 Alias for $res->status.
85 =head2 $res->content_encoding
87 Shortcut for $res->headers->content_encoding.
89 =head2 $res->content_length
91 Shortcut for $res->headers->content_length.
93 =head2 $res->content_type
95 Shortcut for $res->headers->content_type.
97 This value is typically set by your view or plugin. For example,
98 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
99 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
103 Returns a reference to a hash containing cookies to be set. The keys of the
104 hash are the cookies' names, and their corresponding values are hash
105 references used to construct a L<CGI::Cookie> object.
107 $c->response->cookies->{foo} = { value => '123' };
109 The keys of the hash reference on the right correspond to the L<CGI::Cookie>
110 parameters of the same name, except they are used without a leading dash.
111 Possible parameters are:
131 Shortcut for $res->headers->header.
135 Returns an L<HTTP::Headers> object, which can be used to set headers.
137 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
141 Alias for $res->body.
143 =head2 $res->redirect( $url, $status )
145 Causes the response to redirect to the specified URL. The default status is
148 $c->response->redirect( 'http://slashdot.org' );
149 $c->response->redirect( 'http://slashdot.org', 307 );
151 This is a convenience method that sets the Location header to the
152 redirect destination, and then sets the response status. You will
153 want to C< return; > or C< $c->detach() > to interrupt the normal
154 processing flow if you want the redirect to occur straight away.
162 my $location = shift;
163 my $status = shift || 302;
165 $self->location($location);
166 $self->status($status);
169 return $self->location;
172 =head2 $res->location
174 Sets or returns the HTTP 'Location'.
178 Sets or returns the HTTP status.
180 $c->response->status(404);
182 $res->code is an alias for this, to match HTTP::Response->code.
184 =head2 $res->write( $data )
186 Writes $data to the output stream.
192 =head2 $res->print( @data )
194 Prints @data to the output stream, separated by $,. This lets you pass
195 the response object to functions that want to write to an L<IO::Handle>.
203 defined $self->write($data) or return;
206 defined $self->write($,) or return;
207 defined $self->write($_) or return;
215 Catalyst Contributors, see Catalyst.pm
219 This library is free software. You can redistribute it and/or modify
220 it under the same terms as Perl itself.
224 __PACKAGE__->meta->make_immutable;