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(@_) }
29 sub code { shift->status(@_) }
35 Catalyst::Response - stores output responding to the current client request
42 $res->content_encoding;
55 This is the Catalyst Response class, which provides methods for responding to
56 the current client request. The appropriate L<Catalyst::Engine> for your environment
57 will turn the Catalyst::Response into a HTTP Response and return it to the client.
61 =head2 $res->body(<$text|$fh|$iohandle_object)
63 $c->response->body('Catalyst rocks!');
65 Sets or returns the output (text or binary data). If you are returning a large body,
66 you might want to use a L<IO::Handle> type of object (Something that implements the read method
67 in the same fashion), or a filehandle GLOB. Catalyst
68 will write it piece by piece into the response.
72 Predicate which returns true when a body has been set.
76 Alias for $res->status.
78 =head2 $res->content_encoding
80 Shortcut for $res->headers->content_encoding.
82 =head2 $res->content_length
84 Shortcut for $res->headers->content_length.
86 =head2 $res->content_type
88 Shortcut for $res->headers->content_type.
90 This value is typically set by your view or plugin. For example,
91 L<Catalyst::Plugin::Static::Simple> will guess the mime type based on the file
92 it found, while L<Catalyst::View::TT> defaults to C<text/html>.
96 Returns a reference to a hash containing cookies to be set. The keys of the
97 hash are the cookies' names, and their corresponding values are hash
98 references used to construct a L<CGI::Cookie> object.
100 $c->response->cookies->{foo} = { value => '123' };
102 The keys of the hash reference on the right correspond to the L<CGI::Cookie>
103 parameters of the same name, except they are used without a leading dash.
104 Possible parameters are:
124 Shortcut for $res->headers->header.
128 Returns an L<HTTP::Headers> object, which can be used to set headers.
130 $c->response->headers->header( 'X-Catalyst' => $Catalyst::VERSION );
134 Alias for $res->body.
136 =head2 $res->redirect( $url, $status )
138 Causes the response to redirect to the specified URL. The default status is
141 $c->response->redirect( 'http://slashdot.org' );
142 $c->response->redirect( 'http://slashdot.org', 307 );
144 This is a convenience method that sets the Location header to the
145 redirect destination, and then sets the response status. You will
146 want to C< return; > or C< $c->detach() > to interrupt the normal
147 processing flow if you want the redirect to occur straight away.
155 my $location = shift;
156 my $status = shift || 302;
158 $self->location($location);
159 $self->status($status);
162 return $self->location;
165 =head2 $res->location
167 Sets or returns the HTTP 'Location'.
171 Sets or returns the HTTP status.
173 $c->response->status(404);
175 $res->code is an alias for this, to match HTTP::Response->code.
177 =head2 $res->write( $data )
179 Writes $data to the output stream.
185 =head2 $res->print( @data )
187 Prints @data to the output stream, separated by $,. This lets you pass
188 the response object to functions that want to write to an L<IO::Handle>.
196 defined $self->write($data) or return;
199 defined $self->write($,) or return;
200 defined $self->write($_) or return;
208 Catalyst Contributors, see Catalyst.pm
212 This library is free software. You can redistribute it and/or modify
213 it under the same terms as Perl itself.
217 __PACKAGE__->meta->make_immutable;