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 has location => (is => 'rw');
11 has status => (is => 'rw', default => 200);
12 has finalized_headers => (is => 'rw', default => 0);
15 handles => [qw(content_encoding content_length content_type header)],
16 default => sub { HTTP::Headers->new() },
24 clearer => '_clear_context',
27 sub output { shift->body(@_) }
33 Catalyst::Response - stores output responding to the current client request
39 $res->content_encoding;
52 This is the Catalyst Response class, which provides methods for responding to
53 the current client request. The appropriate L<Catalyst::Engine> for your environment
54 will turn the Catalyst::Response into a HTTP Response and return it to the client.
58 =head2 $res->body(<$text|$fh|$iohandle_object)
60 $c->response->body('Catalyst rocks!');
62 Sets or returns the output (text or binary data). If you are returning a large body,
63 you might want to use a L<IO::Handle> type of object (Something that implements the read method
64 in the same fashion), or a filehandle GLOB. Catalyst
65 will write it piece by piece into the response.
69 Predicate which returns true when a body has been set.
71 =head2 $res->content_encoding
73 Shortcut for $res->headers->content_encoding.
75 =head2 $res->content_length
77 Shortcut for $res->headers->content_length.
79 =head2 $res->content_type
81 Shortcut for $res->headers->content_type.
83 This value is typically set by your view or plugin. For example,
84 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
85 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
89 Returns a reference to a hash containing cookies to be set. The keys of the
90 hash are the cookies' names, and their corresponding values are hash
91 references used to construct a L<CGI::Cookie> object.
93 $c->response->cookies->{foo} = { value => '123' };
95 The keys of the hash reference on the right correspond to the L<CGI::Cookie>
96 parameters of the same name, except they are used without a leading dash.
97 Possible parameters are:
115 Shortcut for $res->headers->header.
119 Returns an L<HTTP::Headers> object, which can be used to set headers.
121 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
125 Alias for $res->body.
127 =head2 $res->redirect( $url, $status )
129 Causes the response to redirect to the specified URL. The default status is
132 $c->response->redirect( 'http://slashdot.org' );
133 $c->response->redirect( 'http://slashdot.org', 307 );
135 This is a convenience method that sets the Location header to the
136 redirect destination, and then sets the response status. You will
137 want to C< return; > or C< $c->detach() > to interrupt the normal
138 processing flow if you want the redirect to occur straight away.
146 my $location = shift;
147 my $status = shift || 302;
149 $self->location($location);
150 $self->status($status);
153 return $self->location;
156 =head2 $res->location
158 Sets or returns the HTTP 'Location'.
162 Sets or returns the HTTP status.
164 $c->response->status(404);
166 =head2 $res->write( $data )
168 Writes $data to the output stream.
174 =head2 $res->print( @data )
176 Prints @data to the output stream, separated by $,. This lets you pass
177 the response object to functions that want to write to an L<IO::Handle>.
185 defined $self->write($data) or return;
188 defined $self->write($,) or return;
189 defined $self->write($_) or return;
197 Catalyst Contributors, see Catalyst.pm
201 This program is free software, you can redistribute it and/or modify
202 it under the same terms as Perl itself.
206 __PACKAGE__->meta->make_immutable;