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, lazy => 1, predicate => 'has_body');
11 has location => (is => 'rw');
12 has status => (is => 'rw', default => 200);
13 has finalized_headers => (is => 'rw', default => 0);
16 handles => [qw(content_encoding content_length content_type header)],
17 default => sub { HTTP::Headers->new() },
25 clearer => '_clear_context',
28 sub output { shift->body(@_) }
30 sub code { shift->status(@_) }
36 Catalyst::Response - stores output responding to the current client request
43 $res->content_encoding;
56 This is the Catalyst Response class, which provides methods for responding to
57 the current client request. The appropriate L<Catalyst::Engine> for your environment
58 will turn the Catalyst::Response into a HTTP Response and return it to the client.
62 =head2 $res->body( $text | $fh | $iohandle_object )
64 $c->response->body('Catalyst rocks!');
66 Sets or returns the output (text or binary data). If you are returning a large body,
67 you might want to use a L<IO::Handle> type of object (Something that implements the read method
68 in the same fashion), or a filehandle GLOB. Catalyst
69 will write it piece by piece into the response.
73 Predicate which returns true when a body has been set.
77 Alias for $res->status.
79 =head2 $res->content_encoding
81 Shortcut for $res->headers->content_encoding.
83 =head2 $res->content_length
85 Shortcut for $res->headers->content_length.
87 =head2 $res->content_type
89 Shortcut for $res->headers->content_type.
91 This value is typically set by your view or plugin. For example,
92 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
93 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
97 Returns a reference to a hash containing cookies to be set. The keys of the
98 hash are the cookies' names, and their corresponding values are hash
99 references used to construct a L<CGI::Simple::Cookie> object.
101 $c->response->cookies->{foo} = { value => '123' };
103 The keys of the hash reference on the right correspond to the L<CGI::Simple::Cookie>
104 parameters of the same name, except they are used without a leading dash.
105 Possible parameters are:
125 Shortcut for $res->headers->header.
129 Returns an L<HTTP::Headers> object, which can be used to set headers.
131 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
135 Alias for $res->body.
137 =head2 $res->redirect( $url, $status )
139 Causes the response to redirect to the specified URL. The default status is
142 $c->response->redirect( 'http://slashdot.org' );
143 $c->response->redirect( 'http://slashdot.org', 307 );
145 This is a convenience method that sets the Location header to the
146 redirect destination, and then sets the response status. You will
147 want to C< return > or C<< $c->detach() >> to interrupt the normal
148 processing flow if you want the redirect to occur straight away.
156 my $location = shift;
157 my $status = shift || 302;
159 $self->location($location);
160 $self->status($status);
163 return $self->location;
166 =head2 $res->location
168 Sets or returns the HTTP 'Location'.
172 Sets or returns the HTTP status.
174 $c->response->status(404);
176 $res->code is an alias for this, to match HTTP::Response->code.
178 =head2 $res->write( $data )
180 Writes $data to the output stream.
186 =head2 $res->print( @data )
188 Prints @data to the output stream, separated by $,. This lets you pass
189 the response object to functions that want to write to an L<IO::Handle>.
197 defined $self->write($data) or return;
200 defined $self->write($,) or return;
201 defined $self->write($_) or return;
209 Catalyst Contributors, see Catalyst.pm
213 This library is free software. You can redistribute it and/or modify
214 it under the same terms as Perl itself.
218 __PACKAGE__->meta->make_immutable;